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那一行提示有错,是我的重载出问题了吗?求救大神, 展开
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那一行提示有错,是我的重载出问题了吗?求救大神, 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询