函数重载问题
#include<iostream>using namespace std;
class Complex
{
public:
Complex(){real=0,imag=0;}
Complex(double r,double i){r=real;i=imag;}//设置实部和虚部的变量
Complex operator+(Complex c2);//申明重载函数运算符的额+的函数
void display();//输出函数
private:
double real;
double imag;
};
Complex Complex::operator+(Complex c2)//定义重载运算符+
{ Complex c;
c.real=real+c2.real;
c.imag=imag+c2.imag;
return c;
}
void display() //输出
{ cout<<"("<<real<<","<<imag<<")";}
int main()
{
Complex c1(3,4),c2(5,-10),c3;
c3=c1+c2;
cout<<"c1=";c1.display();
cout<<"c2=";c2.display();
cout<<"c1+c2=";display();
return 0;
}
错误是G:\vc6.0\anzhuang\MSDev98\Bin\重载.cpp(24) : error C2065: 'real' : undeclared identifier
G:\vc6.0\anzhuang\MSDev98\Bin\重载.cpp(24) : error C2065: 'imag' : undeclared identifier
求解决。谢谢