代码是想通过运算符重载实现复数的运算,报错了,大概是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;
}