FRANK_GYZ 2022-03-04 23:12 采纳率: 100%

# C++重载输出运算符出错（输出复数的四则运算）

##### 问题相关代码

1.重载输出函数

``````ostream &operator<<(ostream &out, Plural &p)
{
out<<"{"<<p.real;
if(p.image>0)
out<<"+";
out<<p.image<<"i"<<"}";
return out;
}
``````

2.重载加法运算

``````Plural operator+(Plural a,Plural b)
{
Plural c;
c.real=a.real+b.real;
c.image=a.image+b.image;
return c;
}
``````

3.主函数

``````int main()
{
Plural a,b;
cin>>a;
cin>>b;
cout<<a<<endl;
cout<<b<<endl;

cout<<(a+b)<<endl;
//    cout<<a-b<<endl;
//    cout<<a*b<<endl;
//    cout<<a/b<<endl;
return 0;
}
``````

``````#include<iostream>
using namespace std;
class Plural{

public:
int real,image;
Plural(int a1,int b1)
{
a1=real;
b1=image;
}
Plural()
{

}
friend ostream &operator<<(ostream &out,Plural &p);
friend istream &operator>>(istream &in,Plural &p);
friend Plural operator-(Plural a,Plural b);
friend Plural operator+(Plural a,Plural b);
friend Plural operator*(Plural a,Plural b);
friend Plural operator/(Plural a,Plural b);
};
ostream &operator<<(ostream &out, Plural &p)
{
out<<"{"<<p.real;
if(p.image>0)
out<<"+";
out<<p.image<<"i"<<"}";
return out;
}
istream &operator>>(istream &in,Plural &p)
{
in>>p.real;
in>>p.image;
return in;
}
Plural operator-(Plural a,Plural b)
{
Plural c;
c.real=a.real-b.real;
c.image=a.image-b.image;
return c;
}
Plural operator+(Plural a,Plural b)
{
Plural c;
c.real=a.real+b.real;
c.image=a.image+b.image;
return c;
}
Plural operator*(Plural a,Plural b)
{
Plural c;
c.real=a.real*b.real-a.image*b.image;
c.image=a.real*b.image+a.image*b.real;
return c;
}
Plural operator/(Plural a,Plural b)
{
int d;
d=b.real*b.real-b.image*b.image;
Plural c;
c.real= (a.real*b.real-a.image*b.image)/d;
c.image=(a.real*b.image+a.image*b.real)/d;
}
int main()
{
Plural a,b;
cin>>a;
cin>>b;
cout<<a<<endl;
cout<<b<<endl;
//    cout<<(a+b)<<endl;
//    cout<<a-b<<endl;
//    cout<<a*b<<endl;
//    cout<<a/b<<endl;
return 0;
}

``````
• 输出单个复数（正常）

• 输出复数的加（报错）

• 写回答

#### 2条回答默认 最新

• 关注

因为你在友元函数内试图返回函数内创建的`临时变量`
c在函数返回后就会被销毁,你返回到外部的值没有意义
改为`动态新建`并返回即可
此外你似乎没有考虑到除法会出现分数,如果用int去作为复数的成员,在计算除法时可能出错.
我只简单修改了加法和减法的部分,剩余两种运算同理
示例代码如下
`有帮助望采纳~`

``````
#include <iostream>
using namespace std;
class Plural
{

public:
int real, image;
Plural(int a1, int b1)
{
a1 = real;
b1 = image;
}
Plural()
{
}
friend ostream &operator<<(ostream &out, Plural &p);
friend istream &operator>>(istream &in, Plural &p);
friend Plural &operator-(Plural a, Plural b);
friend Plural &operator+(Plural a, Plural b);
};
ostream &operator<<(ostream &out, Plural &p)
{
out << "{" << p.real;
if (p.image > 0)
out << "+";
out << p.image << "i"
<< "}";
return out;
}
istream &operator>>(istream &in, Plural &p)
{
in >> p.real;
in >> p.image;
return in;
}
Plural &operator-(Plural a, Plural b)
{
Plural *c = new Plural;
c->real = a.real - b.real;
c->image = a.image - b.image;
return *c;
}
Plural &operator+(Plural a, Plural b)
{
Plural *c = new Plural;
c->real = a.real + b.real;
c->image = a.image + b.image;
return *c;
}

int main()
{
Plural a, b;
cin >> a;
cin >> b;
cout << a << endl;
cout << b << endl;
cout << (a + b) << endl;
cout << a - b << endl;

return 0;
}
``````

本回答被题主选为最佳回答 , 对您是否有帮助呢?
评论 编辑记录

• 系统已结题 3月12日
• 已采纳回答 3月4日
• 创建了问题 3月4日

#### 悬赏问题

• ¥15 MATLAB动图输出问题
• ¥50 python如何用抖音这个接口输入抖音ID查询用户信息
• ¥30 关于移动Web网页使用TinyMCE富文本编辑器上传图片后的光标定位、压缩等几个问题如何解决：
• ¥25 activeMq在同服务器centos8下消费很慢
• ¥20 为什么zynq CAN IP 无法进入config配置模式，XCan_SelfTest函数失效？
• ¥15 Pycharm中程序直接运行可以但进入调试报错
• ¥15 MATLAB动图问题
• ¥15 有段代码不知道怎么理解，const isToken = (config.headers || {}).isToken === false
• ¥15 我的显卡支持CUDA最高版本是12.3，这个版本也支持VS 2022 17.0这种情况下如果我想下载CUDA11.8，需要下载旧版本的VS2022吗
• ¥15 pointnet2包安装