c++程序编译错误问题
#include <iostream>
using namespace std;
class String
{public:
String( ){p=NULL;}
String(char *str);
friend bool operator>(String &string1,String &string2);
friend bool operator<(String &string1,String &string2);
friend bool operator==(String &string1,String &string2);
void display( );
private:
char *p;
};
String::String(char *str)
{p=str;}
void String::display( ) //输出p所指向的字符串
{cout<<p;}
bool operator>(String &string1,String &string2)
{if(strcmp(string1.p,string2.p)>0)
return true;
else
return false;
}
bool operator<(String &string1,String &string2)
{if(strcmp(string1.p,string2.p)<0)
return true;
else
return false;
}
bool operator==(String &string1,String &string2)
{if(strcmp(string1.p,string2.p)==0)
return true;
else
return false;
}
void compare(String &string1,String &string2)
{if(operator>(string1,string2)==1)
{string1.display( );cout<<">";string2.display( );}
else
if(operator<(string1,string2)==1)
{string1.display( );cout<<"<";string2.display( );}
else
if(operator==(string1,string2)==1)
{string1.display( );cout<<"=";string2.display( );}
cout<<endl;
}
int main( )
{String string1("Hello"),string2("Book"),string3("Computer"),string4("Hello");
compare(string1,string2);
compare(string2,string3);
compare(string1,string4);
return 0;
}
这是个很简单的运算符重载比较字符大小的程序,但是我的编译器总是编出9个错误,而有的人却不显示错误,什么原因,其中又错误是不能引用私有成员p,但是我已经定义了成员函数为友元函数,为什么还不能引用,高手帮帮忙,我没什么分,但是c++是我唯一能做的事了,帮帮忙,不要吝啬自己的知识,帮助别人也在无形中给自己复习了一下,我就是这样想的,