注册 登录
编程论坛 C++教室

关于C++语言运算符重载的问题!编译报错说存在运算符二义性,请问如何修改?

S140131022 发布于 2015-09-21 20:05, 1280 次点击
#include<iostream>
#include<string>
using namespace std;

class String
{
public:
    String();
    String(char*p);
    void display();
    friend bool operator>(String &a,String &b);
    friend bool operator==(String &a,String &b);
    friend bool operator<(String &a,String &b);
public:
    char *p;
};

bool operator>(String &a,String &b)
{
    if(strcmp(a.p,b.p)>0)
        return true;
    else
        return false;
}

bool operator<(String &a,String &b)
{
    if(strcmp(a.p,b.p)<0)
        return true;
    else
        return false;
}

bool operator==(String &a,String &b)
{
    if(strcmp(a.p,b.p)==0)
        return true;
    else
        return false;
}

void String::display()
{
    cout<<p<<endl;
}

String::String()
{
    p=NULL;
}

String::String(char*x)
{
    p=x;
}

int main()
{
    String a=String("Hello");
    String b=String("Kimi");
    cout<<(a>b)<<endl;
    cout<<(a==b)<<endl;
    cout<<(a<b)<<endl;
}
7 回复
#2
S1401310222015-09-21 20:06
编译信息如下:
Cpp1.cpp
C:\Users\123\Desktop\practice 5\Cpp1.cpp(61) : error C2593: 'operator >' is ambiguous
C:\Users\123\Desktop\practice 5\Cpp1.cpp(62) : error C2593: 'operator ==' is ambiguous
C:\Users\123\Desktop\practice 5\Cpp1.cpp(63) : error C2593: 'operator <' is ambiguous
C:\Users\123\Desktop\practice 5\Cpp1.cpp(64) : warning C4508: 'main' : function should return a value; 'void' return type assumed
执行 cl.exe 时出错.

Cpp1.exe - 1 error(s), 0 warning(s)
#3
诸葛欧阳2015-09-21 20:41
最后一句是主函数没有返回
#4
诸葛欧阳2015-09-21 20:43
忘了友元函数需不需要写String::不过你调用方式好像不对
#5
erty10012015-09-21 21:02
简单说说:

VC6.0 对友员函数支持不够好~!

#include<iostream>
#include<string>
//;using namespace std;
using std::cout;  
using std::endl;  
using std::ostream;  

class String
{
public:
    String();
    String(char*p);
    void display();
    friend bool operator>(String &a,String &b);
    friend bool operator==(String &a,String &b);
    friend bool operator<(String &a,String &b);
protected:
    char *p;
};

bool operator>(String &a,String &b)
{
  if(strcmp(a.p,b.p)>0)
        return true;
    else
        return false;
}

bool operator<(String &a,String &b)
{
    if(strcmp(a.p,b.p)<0)
        return true;
    else
        return false;
}

bool operator==(String &a,String &b)
{
    if(strcmp(a.p,b.p)==0)
        return true;
    else
        return false;
}

void String::display()
{
    cout<<p<<endl;
}

String::String()
{
    p=NULL;
}

String::String(char*x)
{
    p=x;
}

int main()
{
    String a=String("Hello");
    String b=String("Kimi");
    cout<<(a>b)<<endl;
    cout<<(a==b)<<endl;
    cout<<(a<b)<<endl;

    return 0;
}
#6
S1401310222015-09-21 21:25
#include<iostream>
using namespace std;

class Complex
{
public:
    Complex();
    Complex(int,int);
    ~Complex();
    Complex operator+(Complex&);
    friend ostream&operator<<(ostream&,Complex&);
public:
    int real;
    int imag;
};

ostream&operator<<(ostream&output,Complex&exp)
{

    output<<exp.real<<"+"<<exp.imag<<"i"<<endl;
    return output;
}


Complex Complex::operator+(Complex& a)
{
    Complex c;
    c.real=real+a.real;
    c.imag=imag+a.imag;
    return c;
}

Complex::Complex()
{
    real=0;
    imag=0;
}

Complex::~Complex()
{}

Complex::Complex(int a,int b)
{
    real=a;
    imag=b;
}


int main()
{
    Complex a=Complex(3,4);
    Complex b=Complex(4,-10);
    cout<<a<<endl;
    cout<<b<<endl;

}

//又是类似问题! 我明天要换成VC2010啦!
#7
S1401310222015-09-22 09:35
换成VS2010相关问题迎刃而解!
#8
typ9787951652015-09-27 09:32
#include<iostream>
#include<string>
using namespace std;

class String
{
public:
    String() { p = NULL; };
    String(char *x) { p = x; };
    void display();
    friend bool operator>(String &a, String &b);
    friend bool operator==(String &a, String &b);
    friend bool operator<(String &a, String &b);
public:
    char *p;
};

bool operator>(String &a, String &b)
{
    if (strcmp(a.p, b.p)>0)
        return true;
    else
        return false;
}

bool operator<(String &a, String &b)
{
    if (strcmp(a.p, b.p)<0)
        return true;
    else
        return false;
}

bool operator==(String &a, String &b)
{
    if (strcmp(a.p, b.p) == 0)
        return true;
    else
        return false;
}

void String::display()
{
    cout << p << endl;
}



int main()
{
    String a("Hello");
    String b("Kimi");
    cout << (a>b) << endl;
    cout << (a == b) << endl;
    cout << (a<b) << endl;
    system("pause");
    return 0;

我这样子改了一下 就可以了
1