运算符重载的问题
请问下面的程序错在哪里?我在VC++上运行时说运算符重载函数调用不了私有成员,但是不是声明了友元函数了吗?到底怎么回事啊?请高手指点下#include <iostream>
#include <string>
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);
friend void compare(String &string1,String &string2);
void display();
private:
char * p;
};
String::String(char * str)
{
p=str;
}
void String::display()
{
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((string1.p>string2.p)==1)
{
string1.display();cout<<">";string2.display();
}
else
if((string1.p<string2.p)==1)
{
string1.display();cout"<";string2.display();
}
else
if(string1.p==string2.p)==1)
{
string1.display();cout"=";string2.display();
}
cout<<endl;
}
int main()
{
String string1("Hello"),string2("Book"),string3("Computer"),string4("Helle");
compare(string1,string2);
compare(string2,string3);
compare(string3,string4);
return 0;
}