殷其财 2020-12-21 19:07 采纳率: 0%
浏览 14

大佬们,主函数中t3=t1+t2;为什么错了?

#include<iostream>
using namespace std;
class Complex {
private:
	double real;
	double imag;
public:
	Complex();
	Complex(double a, double b);
	Complex operator + (Complex& t1);
	friend istream& operator >>(istream&, Complex&);
	friend ostream& operator <<(ostream&, Complex&);
	Complex operator=(Complex& t);


};
Complex::Complex() {
	real = 0;
	imag = 0;
}
Complex::Complex(double a, double b) {
	real = a;
	imag = b;
}
Complex Complex::operator+(Complex& t1) {
	Complex t;
	t.real = this->real + t1.real;
	t.imag = this->real + t1.imag;
	return t;
}
Complex Complex::operator=(Complex& t) {
	Complex t1;
	t1.real = t.real;
	t1.imag = t.imag;
	return t1;
}
ostream& operator<<(ostream& output, Complex& t) {
	output << "(" << t.real << "+" << t.imag << "i)" << endl;
	return output;
}
istream& operator>>(istream& input, Complex& t) {
	cout << "请输入2个数:" << endl;
	input >> t.real >> t.imag;
	return input;
}

int main() {
	Complex t1, t2, t3, t4;
	cin >> t1 >> t2;
	cout << "t1=" << t1;
	cout << "t2=" << t2;
	t3 = t1+t2;
	t4 = t3;
	cout << "t3=" << t3;
	cout << "t4=" << t4;
	system("pause");
	return 0;
}
  • 写回答

4条回答 默认 最新

  • 空白的墙 2020-12-21 19:34
    关注

    那是个类,不能相加

    评论

报告相同问题?