C++运算符重载,,error C2593: 'operator >' is ambiguous,,错哪了??
#include<iostream>#include<string>usingnamespacestd;classString{public:String(){p=NUL...
#include <iostream>
#include <string>
using namespace std;
class String
{
public:
String(){p=NULL;};
String(char *str);
friend bool operator>(String &strt1,String &str2);
void display();
//private:
public:
char *p; ///字符型指针 用于指向字符串
};
String::String( char *str )
{
p=str;
}
void String::display()
{
//cout<<*p<<endl;
cout<<p<<endl;
}
//bool String::operator>(String &strt1,String &str2) /////友元函数 与 普通成员函数 不同;;;;定义时不需写出类的类名
bool operator>(String &str1,String &str2)
{
//if(strcmp(str1,str2)>0)
if(strcmp(str1.p,str2.p)>0)
// return 1;
return true;
else
// return 0;
return false;
}
int main()
{
String str1("hello"),str2("tom");
//str1.display();
//
//str2.display();
cout<<(str1>str2)<<endl;
return 0;
}
--------------
D:\CC++工作区\CC++语句20140922\运算符重载1.cpp(205) : error C2593: 'operator >' is ambiguous 展开
#include <string>
using namespace std;
class String
{
public:
String(){p=NULL;};
String(char *str);
friend bool operator>(String &strt1,String &str2);
void display();
//private:
public:
char *p; ///字符型指针 用于指向字符串
};
String::String( char *str )
{
p=str;
}
void String::display()
{
//cout<<*p<<endl;
cout<<p<<endl;
}
//bool String::operator>(String &strt1,String &str2) /////友元函数 与 普通成员函数 不同;;;;定义时不需写出类的类名
bool operator>(String &str1,String &str2)
{
//if(strcmp(str1,str2)>0)
if(strcmp(str1.p,str2.p)>0)
// return 1;
return true;
else
// return 0;
return false;
}
int main()
{
String str1("hello"),str2("tom");
//str1.display();
//
//str2.display();
cout<<(str1>str2)<<endl;
return 0;
}
--------------
D:\CC++工作区\CC++语句20140922\运算符重载1.cpp(205) : error C2593: 'operator >' is ambiguous 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询