复数加法、乘法、除法和减法问题 C++ - 运算符重载

Problem with adding, multiplicate, divide and subtract complex numbers C++ - operator overloading

我编写了一个处理复数的程序 - 写入、读取、执行一些简单的操作。 我的老师说我必须定义很多运算符(十八个)。我有一个操作员的问题。

    Comp operator=(const Comp x)
    {
        Comp temp;
        temp.real = re();
        temp.imag = im();
        return temp;
    }

当我将这个运算符放入我的代码中时,加法、乘法、除法和减法都不起作用。 我的输出中有

(1.10,2.00) 
(1.70,3.14) 
2.28
3.57
1.07
1.07
(1.10,-2.00) 
(1.70,-3.14) 
(0.00,0.00) 
(0.00,0.00) 
(0.00,0.00) 
(0.00,0.00) 

而不是

(1.10,2.00) 
(1.70,3.14) 
2.28
3.57
1.07
1.07
(1.10,-2.00) 
(1.70,-3.14) 
(2.80,5.14) 
(-0.60,-1.14) 
(-4.41,6.85) 
(0.64,-0.00)

这是我的全部代码

#include <fstream>
#include <cstdlib> 
#include <iostream>
#include <iomanip>
#include <cmath>
#ifndef M_PI
    #define M_PI 3.14159265358979323846
#endif
using namespace std;

namespace ComplexNumbers
{
class Comp {
    double real, imag;

public:
    Comp(){
    real=0;
    imag=0;
    }
    double re(void) const
    {
        return real;
    }
    double im(void) const
    {
        return imag;
    }
    double mod(void) const
    {
        return sqrt(re()*re() + im()*im());
    }
    double arg(void) const
    {
        double faza;
        if (im() >= 0)
            faza = acos(re()/mod());
        else
            faza = 2*M_PI - acos(re()/mod());

    return faza;
    }
    const Comp conj(void) const
    {
        Comp temp;
        temp.real = re();
        temp.imag = -im();
        return temp;
    }
    ~Comp(){}
    const Comp operator+();
    const Comp operator-();
    bool operator!(void);
    const Comp& operator++()
    { 
        return *this;
    }
    const Comp operator++(int)
    { 
        Comp temp(*this); 
        operator++(); 
        return temp;  
    }
    const Comp& operator--()
    {
        return *this;
    }
    const Comp operator--(int)
    {
        Comp temp(*this); 
        operator--(); 
        return temp; 
    }
    Comp operator=(const Comp x)
    {
        Comp temp;
        temp.real = re();
        temp.imag = im();
        return temp;
    }
    Comp& operator-=(const Comp& x)
    {
        int value = 0;
        value -= x.real;
        value -= x.imag;
        return *this;
    } 
    Comp& operator+=(const Comp& x)
    {
        int value = 0;
        value += x.real;
        value += x.imag;
        return *this;
    }
    Comp& operator*=(const Comp& x)
    {
        int value = 0;
        value *= x.real;
        value *= x.imag;
        return *this;
    }
    Comp& operator/=(const Comp& x)
    {
        int value = 0;
        value /= x.real;
        value /= x.imag;
        return *this;
    } 
    friend const Comp operator+(const Comp& x, const Comp& y)
    {
        Comp temp;
        temp.real = x.real + y.real;
        temp.imag = x.imag + y.imag;
        return temp;
    }
    friend const Comp operator-(const Comp& x, const Comp& y)
    {
        Comp temp;
        temp.real = x.real - y.real;
        temp.imag = x.imag - y.imag;
        return temp;
    }
    friend const Comp operator*(const Comp& x, const Comp& y)
    {
        Comp temp;
        temp.real = (x.real * y.real - x.imag * y.imag);
        temp.imag = (x.real * y.imag + x.imag * y.real);
        return temp;
    }
    friend const Comp operator/(const Comp& x, const Comp& y)
    {
        Comp temp;
        temp.real = ((x.real * y.real) + (x.imag * y.imag))/(y.real*y.real + y.imag*y.imag);
        temp.imag = ((x.imag * y.real) - (x.real * y.imag))/(y.real*y.real + y.imag*y.imag);
        return temp;
    }
    friend bool operator==(const Comp& x, const Comp& y)
    {
        if (x.real == y.real && x.imag == y.imag)
            return 1;
        else
            return 0;
    }
    friend bool operator!=(const Comp& x, const Comp& y)
    {

        if (x.real != y.real || x.imag != y.imag)
            return 1;
        else
            return 0;
    }

    friend std::ostream& operator<<(std::ostream& wart1,  const Comp& a)
    {
        return wart1 <<fixed << setprecision(2) << '(' << a.re() << "," << a.im() << ')' << ' ' << endl;
    }
    friend std::istream& operator>>(std::istream& wart2, Comp& b){
        char c = '0';
        return wart2>>c>>b.real>>c>>b.imag>>c; 
    }
};
}
using namespace ComplexNumbers;
int main(int argc, char* argv[])
{ 
    ifstream read(argv[1]);
    if (!read)
        { cerr << "Open error: " << argv[1] << endl; exit(1);}
    ofstream write(argv[2]);

    if(!write) { cerr << "Open error: " << argv[2] << endl; exit(2);} 
    read.clear();
    read.seekg(0);
    Comp x1;
    read >> x1;
    write << x1;
    cout << x1;
    Comp x2;
    read >> x2;
    write << x2;
    cout << x2;
    cout << x1.mod() << endl;
    cout << x2.mod() << endl;
    cout << x1.arg() << endl;
    cout << x2.arg() << endl;
    cout << x1.conj();
    cout << x2.conj();
    write << x2;
    write << x1.mod() << endl;
    write << x2.mod() << endl;
    write << x1.arg() << endl;
    write << x2.arg() << endl;
    write << x1.conj();
    write << x2.conj();
    Comp sum;
    sum = x1 + x2;
    cout << sum;
    write << sum;
    Comp sub;
    sub = x1 - x2;
    cout << sub;
    write << sub;
    Comp mult;
    mult = x1 * x2;
    cout << mult;
    write << mult;
    Comp div;
    div = x1 / x2;
    cout << div;
    write << div;

    return 0;
}  

你的复制赋值运算符应该赋值给对象本身,像这样:

Comp &operator=(const Comp x)
{
    real = x.real;
    imag = x.imag;
    return *this;
}

另请注意函数签名的变化

您的许多其他运算符(operator-=operator+=operator*=operator/=)都遇到同样的问题,您还应该提供一个复制构造函数('rule of 3').