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条)

报告相同问题?

悬赏问题

  • ¥30 Python调用dll文件输出Nan重置dll状态
  • ¥15 浮动div的高度控制问题。
  • ¥66 换电脑后应用程序报错
  • ¥50 array数据同步问题
  • ¥15 pic16F877a单片机的外部触发中断程序仿真失效
  • ¥15 Matlab插值拟合差分微分规划图论
  • ¥15 keil5 target not created
  • ¥15 C/C++数据与算法请教
  • ¥15 怎么找志同道合的伙伴
  • ¥20 如何让程序ab.eXe自已删除干净硬盘里的本文件自己的ab.eXe文件