求助:运算符重载问题
// 运算符重载的集合应用// p的定义有错误,暂时无法找到原因
#include "stdafx.h"
#include <iostream>
using namespace std;
class String
{
public:
String(char *str):p(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;
};
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(operator>(string1,string2)==1)
{
string1.display();
cout<<">";
string2.display();
}
if(operator<(string1,string2)==1)
{
string1.display();
cout<<"<";
string2.display();
}
if(operator==(string1,string2)==1)
{
string1.display();
cout<<"=";
string2.display'();
}
}
int main()
{
String string1("Hello"),string2("Book");
String string3("Computer"),string4("Hello");
compare(string1,string2);
compare(string2,string3);
compare(string1,string4);
return 0;
}
编译时有很多错误,怀疑p的定义有错误,请问错在了哪里?