amazing_miracle 2015-11-20 03:24 采纳率: 0%
浏览 1742

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

//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()

  • 写回答

3条回答 默认 最新

  • threenewbee 2015-11-20 04:09
    关注

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

    评论

报告相同问题?

悬赏问题

  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样