为什么我定义了变量编译显示未定义 求解
#include<iostream.h>
class complex
{
public: //外部接口
complex(double r=0.0,double i=0.0)
{
real=r;
imag=i;
}
complex operator +(complex c2); //+重载为成员函数
complex operator -(complex c2); // -重载为成员函数
void display(); //输出复数
private:
double real; //复数实部
double imag; //复数虚部
};
complex complex::operator +(complex c2) //重载函数实现
{
complex c;
c.real=c2.real+real;
c.imag=c2.imag+imag;
return complex(c.real,c.imag);
}
complex complex::operator -(complex c2) //重载函数实现
{
complex c;
c.real=c2.real-real;
c.imag=c2.imag-imag;
complex(c.real,c.imag);
}
void display()
{
cout<<"("<<real<<","<<imag<<")"<<endl;
}
void main()
{
complex c1(5,4),c2(2,10),c3;
cout<<"c1=";
c1.display();
cout<<"c2=";
c2.display();
c3=c1-c2; //使用重载运算符完成复数减法
cout<<"c3=c1-c2";
c3.display();
c3=c1+c2; //使用重载运算符完成复数加法
cout<<"c3=c1+c2";
c3.display();
}
error C2065: 'real' : undeclared identifier
error C2065: 'imag' : undeclared identifier
Error executing cl.exe.