C++复数运算符重载后出现问题

DanielT1 2020-05-25 01:11:16
c1,c2,c3,c4都是复数
已经可以使实现
c3=c1+c2;
c3=c1*c2;
但c3=c1+(c1*c2);就会出现

请问这是为什么呢
...全文
159 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
c4 = c1 + (c2 * c3);//c2*c3的结果是个临时值,也就是右值,你的程序只接受传左值引用不接受传右值引用 friend MyComplex operator+(MyComplex& c1, MyComplex&& c2); MyComplex operator+(MyComplex& c1, MyComplex&& c2) { return MyComplex(c2.real + c1.real, c2.imag + c1.imag); }
真相重于对错 2020-05-25
  • 打赏
  • 举报
回复
Complex operator+(double c, Complex& c1) 参数改成const Complex& c1 其余operator也是如此
DanielT1 2020-05-25
  • 打赏
  • 举报
回复

//下面定义用于测试的main()函数
#include "complex.h"
#include <iostream>
using namespace std;

int main()
{
double c = 5;
Complex c1(1,-2), c2(3,-4), c3;
cout << "c="<<c<<endl;
cout << "c1=";
c1.display();
cout << "c2=";
c2.display();

cout << "混合运算:" << endl;
Complex c4;
c3 = c1 + c2;
c4 = c1 * c2;
c4 = c1 + (c2 * c3);
return 0;

}

  • 打赏
  • 举报
回复
main函数看下你的输入
DanielT1 2020-05-25
  • 打赏
  • 举报
回复
抱歉忘了最重要的部分。大佬们请看

#include <iostream>
#define PI acos(-1)
using namespace std;

class Complex
{
public:
double real;
double imag;
// double cabs = double(sqrt(real * real + imag * imag));
// double ctheta = atan(imag / real);

Complex()
{
real = 0;
imag = 0;
}
Complex(double r, double i)
{
real = r;
imag = i;
}

friend Complex operator+(Complex& c1, Complex& c2);
friend Complex operator-(Complex& c1, Complex& c2);
friend Complex operator*(Complex& c1, Complex& c2);
friend Complex operator/(Complex& c1, Complex& c2);
friend Complex operator+(Complex& c1, double c);
friend Complex operator-(Complex& c1, double c);
friend Complex operator*(Complex& c1, double c);
friend Complex operator/(Complex& c1, double c);
friend Complex operator+(double c, Complex& c1);
friend Complex operator-(double c, Complex& c1);
friend Complex operator*(double c, Complex& c1);
friend Complex operator/(double c, Complex& c1);
void display();
Complex ckf(Complex& c1,int n);
double cabs();
double csqrt();
private:

};
Complex operator+(Complex& c1, Complex& c2)
{
return Complex(c2.real + c1.real, c2.imag + c1.imag);
}
Complex operator-(Complex& c1, Complex& c2)
{
return Complex(c1.real - c2.real, c1.imag - c2.imag);
}
Complex operator*(Complex& c1, Complex& c2)
{
return Complex((c1.real * c2.real - c1.imag * c2.imag), (c1.real * c2.imag + c1.imag * c2.real));
}
Complex operator/(Complex& c1, Complex& c2)
{
Complex c;
c.real = ((c1.real * c2.real + c1.imag * c2.imag) / ((c2.real * c2.real) + (c2.imag * c2.imag)));
c.imag = ((c1.imag * c2.real - c1.real * c2.imag) / ((c2.real * c2.real) + (c2.imag * c2.imag)));
return c;
}

Complex operator+(Complex& c1, double c)
{
return Complex(c1.real+c, c1.imag);
}
Complex operator-(Complex& c1, double c)
{
return Complex(c1.real-c, c1.imag);
}
Complex operator*(Complex& c1, double c)
{
return Complex((c * c1.real), (c1.imag * c));
}
Complex operator/(Complex& c1, double c)
{
Complex c3;
c3.real = c1.real / c;
c3.imag = c1.imag / c;
return c3;
}
Complex operator+(double c, Complex& c1)
{
return Complex(c + c1.real, c1.imag);
}
Complex operator-(double c, Complex& c1)
{
return Complex(c-c1.real, -c1.imag);
}
Complex operator*(double c, Complex& c1)
{
return Complex((c * c1.real), (c1.imag * c));
}
Complex operator/(double c, Complex& c1)
{
Complex c3;
c3.real = (c * c1.real) / (c1.real * c1.real + c1.imag * c1.imag);
c3.imag = (-c * c1.imag) / (c1.real * c1.real + c1.imag * c1.imag);
return c3;
}
void Complex::display()
{
cout << "(" << real << "," << imag << "i)" << endl;
}

double cabs(Complex& c1)
{
return(sqrt(c1.real * c1.real + c1.imag * c1.imag));
}

Complex csqrt(Complex& c1)
{
Complex c3;
double ctheta;
if (c1.real > 0)
{
ctheta= atan(c1.imag / c1.real);
}
else if (c1.real == 0 && c1.imag > 0)
{
ctheta = PI / 2;
}
else if (c1.real == 0 && c1.imag < 0)
{
ctheta = -PI / 2;
}
else if (c1.real < 0 && c1.imag >= 0)
{
ctheta = atan(c1.imag / c1.real) + PI;
}
else if (c1.real < 0 && c1.imag < 0)
{
ctheta = atan(c1.imag / c1.real) - PI;
}
c3.real = sqrt(cabs(c1)) * cos(ctheta / 2);
c3.imag = sqrt(cabs(c1)) * sin(ctheta / 2);
return c3;
}
  • 打赏
  • 举报
回复
你重载运算符的声明时传的形参是什么,建议贴代码
Simple-Soft 2020-05-25
  • 打赏
  • 举报
回复
没有代码,不好分析

64,637

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧