关于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;
}
编译器报错,说函数不能访问类的私有数据成员.我明明已经把函数定义为友元了.这是我第一个疑问.
第二个疑问就是名字空间究竟怎么使用?
请各位多多指点,感谢!