求助 这个有关运算符重载程序在编译器不能通过
#include <iostream>using namespace std;
class Complex
{
public:
Complex(int r=0,int i=0);
Complex add(Complex c);
void display();
private:
int real,imag;
};
Complex ::Complex(int r,int i)
{
real=r;imag=i;
}
Complex Complex::operator+(Complex c)
{
Complex temp;
temp.real=real + c.real;
temp.imag=imag + c.imag;
return temp;
}
void Complex::display()
{
char* str;
str=(imag<0)?"":"+";
cout<<real<<str<<imag<<"i"<<endl;
}
int main()
{
Complex c1(2,3),c2(4,5);
Complex c;
c=c1+c2;
cout<<"c1+c2=";
c.display();
return 0;
}
pp1.cpp
C:\Documents and Settings\longtingwei\桌面\c++编译\Cpp1.cpp(16) : error C2039: '+' : is not a member of 'Complex'
C:\Documents and Settings\longtingwei\桌面\c++编译\Cpp1.cpp(4) : see declaration of 'Complex'
C:\Documents and Settings\longtingwei\桌面\c++编译\Cpp1.cpp(19) : error C2248: 'real' : cannot access private member declared in class 'Complex'
C:\Documents and Settings\longtingwei\桌面\c++编译\Cpp1.cpp(10) : see declaration of 'real'
C:\Documents and Settings\longtingwei\桌面\c++编译\Cpp1.cpp(19) : error C2065: 'real' : undeclared identifier
C:\Documents and Settings\longtingwei\桌面\c++编译\Cpp1.cpp(19) : error C2248: 'real' : cannot access private member declared in class 'Complex'
C:\Documents and Settings\longtingwei\桌面\c++编译\Cpp1.cpp(10) : see declaration of 'real'
C:\Documents and Settings\longtingwei\桌面\c++编译\Cpp1.cpp(20) : error C2248: 'imag' : cannot access private member declared in class 'Complex'
C:\Documents and Settings\longtingwei\桌面\c++编译\Cpp1.cpp(10) : see declaration of 'imag'
C:\Documents and Settings\longtingwei\桌面\c++编译\Cpp1.cpp(20) : error C2065: 'imag' : undeclared identifier
C:\Documents and Settings\longtingwei\桌面\c++编译\Cpp1.cpp(20) : error C2248: 'imag' : cannot access private member declared in class 'Complex'
C:\Documents and Settings\longtingwei\桌面\c++编译\Cpp1.cpp(10) : see declaration of 'imag'
C:\Documents and Settings\longtingwei\桌面\c++编译\Cpp1.cpp(33) : error C2676: binary '+' : 'class Complex' does not define this operator or a conversion to a type acceptable to the predefined operator
Error executing cl.exe.
Cpp1.obj - 8 error(s), 0 warning(s)