主题:[讨论]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;
}
这是全部的程序,还请高手指点一二!
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;
}
这是全部的程序,还请高手指点一二!