复数相加问题解决不了
#include<iostream.h>class Complex
{
private:
float real,imag;
public:
Complex(){ real=0;imag=0;}
Complex(float a,float b):real(a),imag(b){}
Complex operator+(Complex &c);
Complex operator +(int &i);
friend ostream& operator<<(ostream &,Complex &);
friend Complex operator +(Complex &,int &);
void display();
};
Complex Complex::operator+(Complex &c)
{
return Complex(real+c.real,imag+c.imag);
}
void Complex::display()
{
cout<<real<<"+"<<imag<<"i"<<endl;
}
ostream &operator<<(ostream &ouput,Complex &c)
{
ouput<<c.real<<"+"<<c.imag<<"i"<<endl;
return ouput;
}
Complex Complex::operator +(int &i)
{
return Complex(real+i,imag);
}
Complex operator+(Complex &c,int &i)
{
return Complex(c.real+i,c.imag);
}
int main()
{
Complex c1(1,2),c2(2,3),c3,c4;
//c3=c1+c2;
c4=c2+4;
cout<<c4;
return 0;
}
--------------------Configuration: 10_3 - Win32 Debug--------------------
Compiling...
10_3.cpp
F:\Study Garden\上机内容\c++\10_3\10_3.cpp(45) : error C2679: binary '+' : no operator defined which takes a right-hand operand of type 'const int' (or there is no acceptable conversion)
执行 cl.exe 时出错.
10_3.obj - 1 error(s), 0 warning(s)