回 帖 发 新 帖 刷新版面

主题:[讨论]C++运算符的重载

这是一个运算符重载的程序,但是运行时候(VC++6.0下),老是出现这样的错误:
error C2679: binary '+' : no operator defined which takes a right-hand operand of type 'const int' (or there is no acceptable convers
下面是源程序:
  #include <iostream.h>
class Complex
{
public:
    Complex(){real=0;image=0;}
    Complex(double ,double );
    Complex operator+(int &);
    friend Complex operator +(Complex &,Complex &);
    void display();
private:
    double real;
    double image;
};
Complex::Complex(double a,double b)
{
    real=a;
    image=b;
}

Complex Complex::operator +(int &a)
{
    return Complex(this->real+a,this->image);
}
Complex operator+(Complex &b,Complex &a)
{
    return Complex(b.real+a.real,b.image+a.image);
}
void Complex::display()
{
    cout<<real<<"+"<<image<<"i"<<endl;
}
int
main(void)
{
    Complex a(1,2),b(2,1),c;
    c=b+2;
    a.display();
    b.display();
    c.display();
    return 0;
}


这是全部的程序,还请高手指点一二!

回复列表 (共1个回复)

沙发


Complex operator+(const int &);
Complex Complex::operator +(const int &a);
这两行改下;

我来回复

您尚未登录,请登录后再回复。点此登录或注册