c++重载问题,为什么我的重载不能运行

#include<iostream>usingnamespacestd;classcomplex{private:doublereal;doubleimag;public... #include<iostream>
using namespace std;
class complex
{
private:
double real;
double imag;
public:
complex();
complex(double r,double i){real=r;i=imag;}
~complex();
//complex operator+( complex &p);

complex operator+(complex &p);
complex operator-(complex &p);
complex operator*(complex &p);
//complex operator!(complex &p);
friend complex operator*(int m,complex &p);
friend ostream& operator<<(ostream &os,complex &p);
friend istream& operator>>(istream &is,complex &p);
//complex operator+(complex &p);
};
complex operator*(int m,complex &p)
{
complex n;
n.real=p.real*m;
n.imag=p.imag*m;
return n;

}
complex::complex()
{
real=imag=0.0;
cout<<"调用构造函数"<<endl;
}

complex::~complex()
{

cout<<"调用析构函数"<<endl;
}

complex complex::operator+(complex &p)
{ double rr;
double ii;
rr=real+p.real;
ii=imag+p.imag;
complex h(rr,ii);
return (h);
}

complex complex::operator-(complex &p)
{
return complex(real-p.real,imag-p.imag);
}

complex complex::operator*(complex &p)
{
complex m;
m.real=real*p.real-(imag*p.imag);
m.imag=real*p.imag+imag*p.real;
return m;
}

//complex complex::operator!(complex &p)
// {
// complex m;
// m.real=p.real;
// m.imag=-p.imag;
// return m;
// }

ostream & operator<<(ostream &os,complex &p)
{
os<<"("<<p.real<<','<<p.imag<<"i"<<")"<<endl;
return os;
}
istream & operator>>(istream &is,complex &p)
{
cout<<"real:";
is>>p.real;
// cout<<endl;
cout<<"imag:";
is>>p.imag;
return is;
}

int main()
{
complex a={3.0,4.0};
complex c;
cout<<"enter a number(q to quit):"<<endl;
while(cin>>c)
{
cout<<"c= "<<c<<'\n'<<endl;
//cout<<"~c= :"<< ~=c<<end;
cout<<"a+c= "<<a+c<<endl;
// cout<<"a*c= "<<a*c<<endl;
// cout<<"a-c= "<<a-c<<endl;
//cout<<"2*c= "<<2*c<<endl;
}
cout<<"END"<<endl;
return 0;
}
为什么a+c那一行提示有错,是我的重载出问题了吗?求救大神,
展开
 我来答
654616566a
2015-08-09 · 超过12用户采纳过TA的回答
知道答主
回答量:18
采纳率:0%
帮助的人:11.9万
展开全部
1、complex a={3.0,4.0}; 改成

complex a(3.0,4.0);

real=r;i=imag; 这里写错了吧,有个卵用

奇奇怪怪的东西,而且代码风格有待改进,比如说 变量命名,局部变量,成员变量,不要说自己看懂就行了(这样就不要贴出来)
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式