主题:C++题编译不通过求助
定义一个复数类,通过重载运算符"+"和"+="来实现两个复数对象的运算.运算符"+="的运算结果为对象的引用,仍可继续参加运算.
#include<iostream.h>
class comp{
float real,ima;
public:
comp(float x=0,float y=0){real=x;ima=y;}
void show(){cout<<real<<'\t'<<ima<<'\n';}
comp& operator += (comp &);
comp operator + (comp &);
};
comp::operator += (comp &c)
{
real+=c.real;ima+=c.ima;
return * this;
}
comp::operator +(comp &c)
{
comp t;
t.real=c.real+real;
t.ima=c.ima+ima;
return t;
}
void main(void)
{
comp c1(200,300),c2(100,200),c3,c4;
c3+=c1+c2;c4+=c3+=c1;
c1.show();c2.show();c3.show();c4.show();
}
这段程序我已看不出问题,但编译无法通过,希望得到高手指点.谢谢!
#include<iostream.h>
class comp{
float real,ima;
public:
comp(float x=0,float y=0){real=x;ima=y;}
void show(){cout<<real<<'\t'<<ima<<'\n';}
comp& operator += (comp &);
comp operator + (comp &);
};
comp::operator += (comp &c)
{
real+=c.real;ima+=c.ima;
return * this;
}
comp::operator +(comp &c)
{
comp t;
t.real=c.real+real;
t.ima=c.ima+ima;
return t;
}
void main(void)
{
comp c1(200,300),c2(100,200),c3,c4;
c3+=c1+c2;c4+=c3+=c1;
c1.show();c2.show();c3.show();c4.show();
}
这段程序我已看不出问题,但编译无法通过,希望得到高手指点.谢谢!