一个C++重载运算符,总说有错误,哪位帮看看,谢谢

2025-01-08 11:01:29
推荐回答(1个)
回答1:

#include
using namespace std;

class Complex //复数类
{
private://私有

    double real;//实数
    double imag;//虚数
public:
    void print()
    {
        cout<        cout<    }
    Complex(double real=0,double imag=0)
    {
        this->real=real;
        this->imag=imag;
    };

    Complex operator+(Complex r);

};

Complex Complex::operator+(Complex r)
{
    return Complex(this->real+r.real,this->imag+r.imag );
}

int main()
{
    Complex com1(5,10),total,t(21,10);
    total=com1+t;

    total.print();
    int a;
    cin>>a;

    return 0;
}