复数的共轭函数

Toi*_*ila 3 c++

我试图创建一个共轭复数的函数,例如A(2,3)将通过键入变成A(2,-3)〜我已经在下面做了一些代码,但我想这是错的,我希望你可以帮我解决这个问题.我在下面的代码中引用了我做错的部分.

#include <iostream>
    using namespace std;
    class Complex
    {
    private:
        double real;
        double imaginenary;
    public:
        Complex();
        Complex(double r, double i = 0);

        // Declaration 
        Complex operator~(const Complex & c) const;


        Complex operator+(const Complex & c) const;
        Complex operator-(const Complex & c) const;
        Complex operator*(const Complex & c) const;
        Complex operator*(double n) const; 
       friend Complex operator*(double m, const Complex & c)
               { return c * m; }    
        friend ostream & operator<<(ostream & os, const Complex & c);
    };
    Complex::Complex()
    {
        real = imaginenary = 0;
    }

    Complex::Complex(double r, double i )
    {
        real = r;
        imaginenary = i;
    }



    // Definition
    Complex Complex::operator~(const Complex & c) const   
    {
        Complex conj;
        conj.imaginenary = -1 * imaginenary;
        conj.real = real;
    }


    Complex Complex::operator+(const Complex & c) const
    {
        Complex sum;
        sum.imaginenary = imaginenary + c.imaginenary;
        sum.real = real + c.real;
        return sum;
    }

    Complex Complex::operator-(const Complex & c) const
    {
        Complex diff;
        diff.imaginenary = imaginenary - c.imaginenary;
        diff.real = real - c.real;
        return diff;
    }
    Complex Complex::operator*(const Complex & c) const
    {
        Complex mult;
        mult.imaginenary = imaginenary * c.imaginenary;
        mult.real = real * c.real;
        return mult;
    }

    Complex Complex::operator*(double mult) const
    {
        Complex result;
        result.real = real * mult;
        result.imaginenary = imaginenary * mult;
        return result;
    }
    ostream & operator<<(ostream & os, const Complex & c)
    {
        os << "(" << c.real <<"," << c.imaginenary<<"i)";
        return os;
    }
    int main()
    {
        Complex A;
        Complex B(5, 40);
        Complex C(2, 55);
        cout << "A, B, and C:\n";
        cout << A <<"; " << B << ": " << C << endl;
        cout << " complex conjugate is" << ~C << endl;  
        cout << "B + C: " << B+C << endl;
        cout << "B * C: " << B*C << endl;
        cout << "10 * B: " << 10*B << endl;
        cout << "B - C: " << B - C << endl;
        return 0;
    }
Run Code Online (Sandbox Code Playgroud)

Mot*_*tti 9

波浪号(〜)运算符是一元运算符,因此它不应接受参数(它可以工作*this).你也忘了从中返回一个值operator~.

Complex operator~() const 
{
    return Complex( real, -1 * imaginenary);
}
Run Code Online (Sandbox Code Playgroud)

在这里查看您的固定代码

顺便说一句:这是拼写想象的.