FRANK_GYZ 2022-03-04 23:12 采纳率: 100%
浏览 32
已结题

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;
}

运行结果及报错内容
  • 输出单个复数(正常)

img

  • 输出复数的加(报错)

img

我想要达到的结果

怎么样通过重载运算符来输出两个复数的和,不能添加第三个变量。

  • 写回答

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;
    }
    

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

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

悬赏问题

  • ¥15 前端加access数据库
  • ¥15 ARCGIS 多值提取到点 ERROR 999999
  • ¥15 mysql异常断电, [MY-011971] [InnoDB]
  • ¥15 uni.onBluetoothDeviceFound熄屏不运行
  • ¥35 关于shodan搜索网络摄像头的各种方法详解
  • ¥15 求PHDA糖尿病并发症数据集,有偿
  • ¥15 为什么AVL fire DVI 界面里面的response Editor project 中的Summary result 点不了
  • ¥20 中标麒麟系统V4.0,linux3.10.0的内核,3A4000处理器,如何安装英伟达或AMD的显卡驱动,
  • ¥20 求文心中文心理分析系统(TextMind)
  • ¥15 chipyard环境搭建问题