求大神帮我改一下错误,感激不尽

//1、定义复数类Complex,使其包含两个私有数据成员:实部float real和虚部float imag;
//2、定义并实现该类的带参数构造函数且带有缺省值0;
//3、定义并实现能够完成两个Complex类对象加法的运算符重载函数;
//4、定义并实现能够输出Complex类对象的流插入符的运算符重载函数;
//5、在main函数中对上述设计进行测试。
#include
using namespace std;
class Complex{
private:
float real;
float imag;
public:
Complex()
{
real=0;imag=0;
}
Complex(float r,float i)
{
real=r;imag=i;
}
friend Complex operator+(const Complex&c1,const Complex&c2){
Complex c;
c.real=c1.real+c2.real;
c.imag=c1.imag+c2.imag;
return c;
}
friend ostream &operator<<(ostream&output,Complex &c){
output< if(c.imag>0){
output<<"+";
output<<c.imag<<"i";
}
else if(c.imag==0){
output<<endl;}
else if(c.imag<0){
output<<c.imag<<"i";
return output;}
}
};
int main(){

Complex c1,c2(2),c3(3,4),c4(3,-5),c5;
c5=c2+c4;
cout<<"c1="<<c1<<endl;
cout<<"c2="<<c2<<endl;
return 0;
}
[Error] C:\Users\Administrator\Documents\C-Free\Temp\未命名1.cpp:42: error: no matching function for call to `Complex::Complex(int)'
[Warning] C:\Users\Administrator\Documents\C-Free\Temp\未命名1.cpp:8: note: candidates are: Complex::Complex(const Complex&)
[Warning] C:\Users\Administrator\Documents\C-Free\Temp\未命名1.cpp:18: note: Complex::Complex(float, float)
[Warning] C:\Users\Administrator\Documents\C-Free\Temp\未命名1.cpp:14: note: Complex::Complex()

c++

3个回答

c2(2)
你调用的这个构造函数,但是你没有任何构造函数接收仅仅一个int参数。
是不是c2(1, 2)之类的。少写了什么

可以参考下我在用的

 //#include "stdafx.h"
#include <iostream>
class comp
{
double a;
double b;
public:
comp(double x=0,double y=0):a(x),b(y){}
comp operator+(comp &x)
{
comp c;
c.a=a+x.a;
c.b=b+x.b;
return c;
}
comp operator-(comp &x)
{
comp c;
c.a=a-x.a;
c.b=b-x.b;
return c;
}
comp operator*(comp &x)
{
comp c;
c.a=a*x.a-b*x.b;
c.b=a*x.b+b*x.a;
return c;
}
comp operator/(comp &x)
{
comp c;
double temp;
temp=x.a*x.a+x.b*x.b;

if (temp==0)
printf("can't div zero!");

c.a=(a*x.a+b*x.b)/temp;
c.b=(b*x.a-a*x.b)/temp;
return c;
}
void input(double x,double y)
{a=x;
b=y;
}
void output()
{printf("%f+%fi\n",a,b);
}

double getreal()
{
    return a;
}
double getimag()
{
    return b;
}

};

没有对应的构造函数啊,这是失误吗??????????????????????????????????????????????

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问