QiQiQi__ 2021-05-18 20:00 采纳率: 50%
浏览 31
已采纳

C++请问下面的代码错在哪里,有关运算符重载

代码是想通过运算符重载实现复数的运算,报错了,大概是operator+, operator-那里的问题,但我不知道怎么改。(省去了头文件)

#include<iostream>
#include<string>
#include<cmath>
#include"Complex1.h"
using namespace std;

Complex::Complex(double real=0,double imag=0){
    re=real;im=imag;
}
Complex::Complex(const Complex& orig){
    re=orig.re;im=orig.im;
}
Complex& Complex::operator=(const Complex& arg){
    re=arg.re;im=arg.im;
    return *this;
}
Complex Complex::operator+(const Complex& arg){
    re+=arg.re;im+=arg.im;
    return re,im;
}
Complex Complex::operator-(const Complex& arg){
    re-=arg.re;im-=arg.im;
    return re,im;
}
Complex Complex::operator*(const Complex& arg){
    re=re*arg.re-im*arg.im;
    im=re*arg.im-arg.re*im;
    return re,im;
}
Complex Complex::operator/(const Complex& arg){
    re=(re*arg.re+im*arg.im)/(arg.re*arg.re+arg.im*arg.im);
    im=(im*arg.re-re*arg.im)/(arg.re*arg.re+arg.im*arg.im);
    return re,im;
}
Complex& Complex::operator+=(const Complex& arg){
    re+=arg.re;im+=arg.im;
    return *this;
}
Complex Complex::operator-=(const Complex& arg){
    re-=arg.re;im-=arg.im;
    return *this;
}
Complex Complex::operator*=(const Complex& arg){
    re=re*arg.re-im*arg.im;
    im=re*arg.im-arg.re*im;
    return *this;
}
Complex Complex::operator/=(const Complex& arg){
    re=(re*arg.re+im*arg.im)/(arg.re*arg.re+arg.im*arg.im);
    im=(im*arg.re-re*arg.im)/(arg.re*arg.re+arg.im*arg.im);
    return *this;
}
bool Complex::operator==(const Complex& arg){
    return (re==arg.re)&&(im==arg.re);
}
bool Complex::operator!=(const Complex& arg){
    return (re!=arg.re)||(im!=arg.re);
}
double Complex::moduls(){
    return sqrt(re*re+im*im);
}
Complex::string str(){
    stringstream ss;
    ss<<re<<"+"<<im<<"i";
    return ss.str;
}
  • 写回答

2条回答 默认 最新

  • qzjhjxj 2021-05-18 20:33
    关注

    修改如下,供参考:

    #include<iostream>
    #include<string>
    #include<cmath>
    #include"Complex1.h"
    using namespace std;
    
    Complex::Complex(double real=0,double imag=0){
        re=real;im=imag;
    }
    Complex::Complex(const Complex& orig){
        re=orig.re;im=orig.im;
    }
    Complex& Complex::operator=(const Complex& arg){
        re=arg.re;im=arg.im;
        return *this;
    }
    Complex Complex::operator+(const Complex& arg){
        Complex tmp;
        tmp.re=re+arg.re;tmp.im=im+arg.im;
        return  tmp;
    }
    Complex Complex::operator-(const Complex& arg){
        Complex tmp;
        tmp.re=re-arg.re;tmp.im=im-arg.im;
        return  tmp;
    }
    Complex Complex::operator*(const Complex& arg){
        Complex tmp;
        tmp.re=re*arg.re-im*arg.im; //这里实部、虚部都是减吗?
        tmp.im=re*arg.im-arg.re*im;
        return  tmp;
    }
    Complex Complex::operator/(const Complex& arg){
        Complex tmp;
        tmp.re=(re*arg.re+im*arg.im)/(arg.re*arg.re+arg.im*arg.im);
        tmp.im=(im*arg.re-re*arg.im)/(arg.re*arg.re+arg.im*arg.im);
        return  tmp;
    }
    Complex& Complex::operator+=(const Complex& arg){
        re+=arg.re;im+=arg.im;
        return *this;
    }
    Complex Complex::operator-=(const Complex& arg){
        re-=arg.re;im-=arg.im;
        return *this;
    }
    Complex Complex::operator*=(const Complex& arg){
        re=re*arg.re-im*arg.im;
        im=re*arg.im-arg.re*im;
        return *this;
    }
    Complex Complex::operator/=(const Complex& arg){
        re=(re*arg.re+im*arg.im)/(arg.re*arg.re+arg.im*arg.im);
        im=(im*arg.re-re*arg.im)/(arg.re*arg.re+arg.im*arg.im);
        return *this;
    }
    bool Complex::operator==(const Complex& arg){
        return (re==arg.re)&&(im==arg.im);//(im==arg.re);
    }
    bool Complex::operator!=(const Complex& arg){
        return (re!=arg.re)||(im!=arg.im);//(im!=arg.re);
    }
    double Complex::moduls(){
        return sqrt(re*re+im*im);
    }
    Complex::string str(){
        stringstream ss;
        ss<<re<<"+"<<im<<"i";
        return ss.str;
    }
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况