<complex>类复数相加问题,求解
#include<iostream>#include<complex>
using namespace std;
class Complex
{
private:
complex <double> Complex_add;
public:
Complex() : Complex_add(0, 0)
{
cout << "Initialize!" << endl;
}
friend void add(Complex&, complex <double>&, complex <double>&);
void output();
~Complex()
{
cout << "Set free!" << endl;
}
};
void add(Complex& num, complex <double>& num1, complex <double>& num2)
{
= num1.real() + num2.real();
= num1.imag() + num2.imag();
}
void Complex :: output()
{
cout << "The real is" << Complex_add.real() << endl;
cout << "The imag is" << Complex_add.imag() << endl;
}
int main()
{
Complex Complex_add;
complex <double> num1(2.3, 3.4);
complex <double> num2(4.11, 3.45);
add(Complex_add, num1, num2);
Complex_add.output();
cout << endl;
return 0;
}
运行之后只加了虚部
实部还是0
各位帮帮忙