书本上的例程,但是我按照书上的敲上去结果却不对……
//这个是头文件
#ifndef STRING1_H_
#define STRING1_H_
#include <iostream>
using std::ostream;
using std::istream;
class String
{
private:
char *str;
int len;
static int num_strings;
static const int CINLIM = 80;
public:
String(const char *s);
String();
String(const String &);
~String();
int length()const{return len;}
String & operator=(const String &);
String & operator=(const char *);
char & operator[](int i);
const char & operator[](int i) const;
friend bool operator<(const String &st,const String & st2);
friend bool operator>(const String &st,const String & st2);
friend bool operator==(const String &st,const String & st2);
friend ostream & operator<<(ostream & os,const String & st);
friend istream & operator>>(istream & is,const String & st);
static int HowMany();
};
#endif
//这个是类的实现文件
#include <cstring>
#include "string1.h"
using std::cin;
using std::cout;
int String::num_strings=0;
int String::HowMany()
{
return num_strings;
}
String::String(const char *s)
{
len=strlen(s);
str=new char[len+1];
strcpy(str,s);
num_strings++;
}
String::String()
{
len =4;
str=new char[1];
str[0]='\0';
num_strings++;
}
String::String(const String &st)
{
num_strings++;
len=st.len;
str=new char[len+1];
strcpy(str,st.str);
}
String::~String()
{
--num_strings;
delete []str;
}
String & String::operator =(const String & st)
{
if(&st==this)
return *this;
delete []str;
len=st.len;
str=new char[len+1];
strcpy(str,st.str);
return *this;
}
String &String::operator =(const char *s)
{
delete []str;
len=strlen(s);
str=new char[len+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 & st,const String &st2)
{
return(strcmp(st.str,st2.str)<0);
}
bool operator==(const String & st,const String & st2)
{
return(strcmp(st.str,st2.str)==0);
}
bool operator>(const String &st,const String &st2)
{
return st2.str<st.str;
}
ostream & operator<<(ostream & os,const String &st)
{
os<<st.str;
return os;
}
istream & operator>>(istream & is,String & st)
{
char temp[String::CINLIM];
is.get(temp,String::CINLIM);
if(is)
st=temp;
while (is && is.get()!='\n')
{
continue;
}
return is;
}
//测试文件
#include <iostream>
#include "string1.h"
const int ArSize=10;
const int MaxLen=81;
int main()
{
using std::cout;
using std::cin;
using std::endl;
String name;
cout<<"Hi, what's your name?\n";
cin>>name;
cout<<name<<",please enter up to "<< ArSize
<<" short sayings <empty line to quit>:\n";
String sayings[ArSize];
char temp[MaxLen];
int i;
for(i=0;i<ArSize;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 sayingsL\n";
for(i=0;i<total;i++)
cout<<sayings[i][0]<<": "<<sayings[i]<<endl;
int shortest=0;
int first = 0;
for (i=1;i<total;i++)
{
if (sayings[i].length()<sayings[shortest].length())
{
shortest=i;
}
if (sayings[i]<sayings[first])
{
first = i;
}
}
cout<<"Shortest saying:\n"<<sayings[shortest]<<endl;
cout<<"First alphabetically:\n"<<sayings[first]<<endl;
cout<<"This program used "<<String::HowMany()
<<" String objects.Bye.\n";
return 0;
}
以下是编译信息
Compiling...
sayings1.cpp
e:\c++ primer范例\cp12\string\string1.h(12) : error C2258: illegal pure syntax, must be '= 0'
e:\c++ primer范例\cp12\string\string1.h(12) : error C2252: 'CINLIM' : pure specifier can only be specified for functions
string1.cpp
e:\c++ primer范例\cp12\string\string1.h(12) : error C2258: illegal pure syntax, must be '= 0'
e:\c++ primer范例\cp12\string\string1.h(12) : error C2252: 'CINLIM' : pure specifier can only be specified for functions
e:\c++ primer范例\cp12\string\string1.cpp(81) : error C2039: 'CINLIM' : is not a member of 'String'
e:\c++ primer范例\cp12\string\string1.h(7) : see declaration of 'String'
e:\c++ primer范例\cp12\string\string1.cpp(81) : error C2065: 'CINLIM' : undeclared identifier
e:\c++ primer范例\cp12\string\string1.cpp(81) : error C2057: expected constant expression
e:\c++ primer范例\cp12\string\string1.cpp(81) : error C2466: cannot allocate an array of constant size 0
e:\c++ primer范例\cp12\string\string1.cpp(81) : error C2133: 'temp' : unknown size
e:\c++ primer范例\cp12\string\string1.cpp(82) : error C2039: 'CINLIM' : is not a member of 'String'
e:\c++ primer范例\cp12\string\string1.h(7) : see declaration of 'String'
执行 cl.exe 时出错.
1.exe - 1 error(s), 0 warning(s)
看起来问题都出现再红色字体的地方,怎么会这样呢
我用的是VC6.0,会不会是因为这个问题。
[此贴子已经被作者于2007-9-5 21:09:29编辑过]