| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1495 人关注过本帖
标题:关于String类实现的几个问题
只看楼主 加入收藏
zzt_428
Rank: 2
来 自:南京师范大学
等 级:论坛游民
威 望:2
帖 子:243
专家分:22
注 册:2008-7-6
收藏
 问题点数:0 回复次数:7 
关于String类实现的几个问题
我弄了一个程序,,实现String类简单的几个功能.但是调试了很长时间没有调试通过.
各位请多多指点,看究竟错在怎么地方.
程序功能是使用对象数组,保存十个字符串,然后显示出来,并且打印出最大的字符串.

//String.h--头文件,包含了class String 的声明

#include <iostream>
using namespace std;

#ifndef STRING_H_
#define STRING_H_


class String
{
public:
    String(const char *s);
    String();
    String(const String &);
    ~String();
    int Length()const
    {
        return length;
    }

    String & operator=(const char *);
    String & operator=(const String &);
    char & operator[](int i);
    const char & operator[](int i)const;

    friend bool operator<(const String &st1, const String st2);
    friend bool operator>(const String &st1, const String &st2);
    friend bool operator==(const String &st1, const String st2);
    friend ostream & operator<<(ostream &os, const String &st);
    friend istream & operator>>(istream &is, String &st);

    static int HowMany();

private:
    enum{CINLIM=90};
    char *str;
    int length;
    static int numStrings;
    

};
#endif

//String.cpp--String 类成员函数的实现

#include <cstring>
#include "String.h"

using namespace std;
int String::numStrings=0;

//static method
int String::HowMany()
{
    return numStrings;
}

//class method

String::String(const char *s)
{
    length=strlen(s);
    str=new char[length+1];
    strcpy(str,s);
    numStrings++;
}

String::String()
{
    length=4;
    str=new char[1];
    str[0]='\0';
    numStrings++;
}

String::String(const String &st)
{
    numStrings++;
    length=st.length;
    str=new char[length+1];
    strcpy(str,st.str);
}

String::~String()
{
    --numStrings;
    delete [] str;
}

//overloaded operator methods

String & String::operator =(const String &st)
{
    if(this == &st)
        return *this;
    delete [] str;
    length=st.length;
    str=new char[length+1];
    strcpy(str,st.str);
    return *this;
}

String & String::operator =(const char *s)
{
    delete [] str;
    length=std::strlen(s);
    str=new char[length+1];
    strcpy(str,s);
    return *this;
}

char & String::operator [](int i)
{
    return str[i];
}

const char & String::operator [](int i)const
{
    return str[i];
}

bool operator<(const String &st1, const String &st2)
{
    return (std::strcmp(st1.str,st2.str)<0);
}

bool operator>(const String &st1, const String &st2)
{
    return st2.str<st1.str;
}

bool operator ==(const String st1, const String &st2)
{
    return (std::strcmp(st1.str,st2.str)==0);
    
}

ostream & operator<<(ostream &os, const String &st)
{
    os << st.str;
    return os;
}

istream & operator>>(istream &is, const String &st)
{
    char temp[CINLIM];
    is.get(temp,CINLIM);
    if(is)
        st=temp;
    while(is && is.get()!='\n')
        continue;
    return is;
}


//UseString.cpp --使用String 类
#include <iostream>
#include "String.h"
const int ArrSize=10;
const int MaxLen=81;

int main()
{
    using std::cin;
    using std::cout;
    using std::endl;
    String name;
    cout<< "Hi What is your name:\n";
    cin >> name;
    cout << name << "Please enter up to" << ArrSize << "short sayings(q to quit):";
    String sayings[ArrSize];
    char temp[MaxLen];
    int i;
    for(i=0; i<ArrSize; i++)
    {
        cout << i+1 << "@";
        cin.get(temp, MaxLen);
        while(cin && cin.get()!='\n')
            continue;
        if(!cin || temp[0]=='\0')
            break;
        else
            sayings[i]=temp;
    }
    int total=i;
    cout << "Here are your sayings:\n";
    for(i=0; i<total; i++)
        cout << sayings[i][0] << ":" << sayings[i] << endl;

    int shortest=0;
    int first=0;

    for(i=0; i<total; i++)
    {
        if(sayings[i].Length() < sayings[shortest].Length())
            shortest=i;
        if(sayings[i] < sayings[first])
            first=i;
    }

    cout << "shortest sayings:" << sayings[shortest] << endl;
    cout << "first sayings:" << sayings[first] << endl;
    cout << "The program used " << String::HowMany() << "string objects. bye!";

    return 0;
}

编译器报错,说函数不能访问类的私有数据成员.我明明已经把函数定义为友元了.这是我第一个疑问.
第二个疑问就是名字空间究竟怎么使用?
请各位多多指点,感谢!
搜索更多相关主题的帖子: String 
2008-09-13 17:36
中学者
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:20
帖 子:3554
专家分:80
注 册:2007-9-14
收藏
得分:0 
我想你用的应该是VC6.0..这是VC6.0的一个bug...

樱花大战,  有爱.
2008-09-13 19:18
zzt_428
Rank: 2
来 自:南京师范大学
等 级:论坛游民
威 望:2
帖 子:243
专家分:22
注 册:2008-7-6
收藏
得分:0 
thank you
楼上说对了!内存小,VS 装不起来..嘿嘿
2008-09-15 21:45
xianshizhe111
Rank: 1
等 级:新手上路
帖 子:1451
专家分:0
注 册:2007-12-8
收藏
得分:0 
vs2003 小一点
2008-09-17 12:03
zzt_428
Rank: 2
来 自:南京师范大学
等 级:论坛游民
威 望:2
帖 子:243
专家分:22
注 册:2008-7-6
收藏
得分:0 
在此谢过楼上两位!
2008-09-17 17:52
ppg
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2008-9-17
收藏
得分:0 
1>d:\backup\我的文档\visual studio 2005\projects\pp\pp\string.h(27) : error C2804: binary 'operator ==' has too many parameters
2008-09-18 10:52
ppg
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2008-9-17
收藏
得分:0 
1>d:\backup\我的文档\visual studio 2005\projects\pp\pp\string.h(27) : error C2804: binary 'operator ==' has too many parameters
是怎么回事啊?
2008-09-18 10:52
zzt_428
Rank: 2
来 自:南京师范大学
等 级:论坛游民
威 望:2
帖 子:243
专家分:22
注 册:2008-7-6
收藏
得分:0 
回答
楼上的提示多了一个参数,其实一个参数也可以,两个参数也可以,看用哪个对象来调用

学习切忌浮躁.
2008-12-10 19:54
快速回复:关于String类实现的几个问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016185 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved