首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

为什么小弟我定义了变量编译显示未定义 求解

2012-05-24 
为什么我定义了变量编译显示未定义 求解#includeiostream.hclass complex{public://外部接口complex(dou

为什么我定义了变量编译显示未定义 求解
#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.

[解决办法]
display()定义前少了complex::
[解决办法]
void display()
-> 
void complex::display()

热点排行