| 编程中国 | 业界新闻 | 技术文章 | 视频教程 | 下载频道 | 程序源码 | 个人空间 | 编程论坛
全能ASP/PHP/ASP.NET主机,支持月付专业 MSSQL 数据库空间,支持月付专业 MySQL 数据库空间,支持月付买域名,送MP3、MP4
高端软件开发 = 年薪十万不是梦赛孚耐:软件保护加密专家身份认证令牌USB KEY买空间,免费送域名(厦门中资源)
共有 322 人关注过本帖
标题:关于String类实现的几个问题
收藏  订阅  推荐  打印 
zzt_428
Rank: 2
来自:南京师范大学
等级:注册会员
威望:1
帖子:156
积分:1740
注册:2008-7-6
关于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-9-13 17:36
中学者
Rank: 12Rank: 12Rank: 12
等级:版主
威望:11
帖子:3376
积分:34562
注册:2007-9-14

我想你用的应该是VC6.0..这是VC6.0的一个bug...

汇编.....
2008-9-13 19:18
zzt_428
Rank: 2
来自:南京师范大学
等级:注册会员
威望:1
帖子:156
积分:1740
注册:2008-7-6
thank you

楼上说对了!内存小,VS 装不起来..嘿嘿
2008-9-15 21:45
xianshizhe111
Rank: 6Rank: 6
等级:金牌会员
帖子:1451
积分:15818
注册:2007-12-8

vs2003 小一点
2008-9-17 12:03
zzt_428
Rank: 2
来自:南京师范大学
等级:注册会员
威望:1
帖子:156
积分:1740
注册:2008-7-6

在此谢过楼上两位!
2008-9-17 17:52
ppg
Rank: 1
等级:新手上路
帖子:9
积分:174
注册:2008-9-17

1>d:\backup\我的文档\visual studio 2005\projects\pp\pp\string.h(27) : error C2804: binary 'operator ==' has too many parameters
2008-9-18 10:52
ppg
Rank: 1
等级:新手上路
帖子:9
积分:174
注册:2008-9-17

1>d:\backup\我的文档\visual studio 2005\projects\pp\pp\string.h(27) : error C2804: binary 'operator ==' has too many parameters
是怎么回事啊?
2008-9-18 10:52
关于我们 | 广告合作 | 编程中国 | 清除Cookies | Archiver | WAP | TOP

编程中国 版权所有,并保留所有权利。鲁ICP备08000592号
Powered by Discuz, Processed in 0.054200 second(s), 9 queries.
Copyright©2004-2008, BCCN.NET, All Rights Reserved