不太清楚 你的意思,下面是修改后的代码
#include
using namespace std;
class Complex
{
public:
Complex(){real=0;imag=0;}
Complex(double r,double i):real(r),imag(i){}
operator double(){return real;}
friend ostream& operator<<(ostream& output,Complex& c);
void change(double r);
protected:
double real,imag;
};
ostream& operator<<(ostream& output,Complex& c)
{
output << c.real;
if(c.imag==0)// return output; //这里实在是搞不清楚你的意思 output+ ?
output << c.imag << endl;
return output;
}
void Complex::change(double r)
{
real=r;
imag=0;
}
int main()
{
Complex c1(3,6),c2;
double a=5.7,d1;
d1=a+c1;
cout << d1 << endl;
c2.change(d1);
cout << c2 << endl;
return 0;
}
不太清楚 你的意思,下面是修改后的代码
#include
using namespace std;
class Complex
{
public:
Complex(){real=0;imag=0;}
Complex(double r,double i):real(r),imag(i){}
operator double(){return real;}
friend ostream& operator<<(ostream& output,Complex& c);
void change(double r);
protected:
double real,imag;
};
ostream& operator<<(ostream& output,Complex& c)
{
output << c.real;
if(c.imag==0)// return output; //这里实在是搞不清楚你的意思 output+ ?
output << c.imag << endl;
return output;
}
void Complex::change(double r)
{
real=r;
imag=0;
}
int main()
{
Complex c1(3,6),c2;
double a=5.7,d1;
d1=a+c1;
cout << d1 << endl;
c2.change(d1);
cout << c2 << endl;
return 0;
你的#includeiostream
using namespace std;
class Complex
{
Complex(){real=0;imag=0;}
Complex(double r,double i)real(r),imag(i){}
operator double(){return real;}
friend ostream& operator (ostream& output,Complex& c);
void change(double r);
private
double real,imag;
};
ostream& operator (ostream& output,Complex& c)
{
output(c.real;
if(c.imag=0) output+;
outputc.imagi)endl;
return output;
}
void Complexchange(double r)
{
real=r;
imag=0;
}
int main()
{
Complex c1(3,6),c2;
double a=5.7,d1;
d1=a+c1;
coutd1endl;
c2.change(d1);
coutc2endl;
return 0;
修改后:
#include
using namespace std;
class Complex
{
public:
Complex(){real=0;imag=0;}
Complex(double r,double i):real(r),imag(i){}
Complex const operator +(Complex &x);
friend ostream& operator<<(ostream& output,Complex& c)
{
if(c.imag!=0) output<
output<
}
void change(Complex r);
private:
double real,imag;
};
Complex const Complex::operator +(Complex &x)
{
Complex tem;
tem.real = this->real + x.real;
tem.imag = this->imag + x.imag;
return tem;
}
void Complex::change(Complex r)
{
real=r.real;
imag=r.imag;
}
int main()
{
Complex c1(3,6),c2;
Complex a(5.7,0),d1;
d1=a+c1;
cout<
cout<
}
头文件少个尖括号