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

大佬们,主函数中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
    关注

    那是个类,不能相加

    评论

报告相同问题?

悬赏问题

  • ¥15 pcl运行在qt msvc2019环境运行效率低于visual studio 2019
  • ¥15 MAUI,Zxing扫码,华为手机没反应。可提高悬赏
  • ¥15 求帮看看那里的问题ssh项目报错
  • ¥15 python运行报错 ModuleNotFoundError: No module named 'torch'
  • ¥100 华为手机私有App后台保活
  • ¥15 sqlserver中加密的密码字段查询问题
  • ¥20 有谁能看看我coe文件到底哪儿有问题吗?
  • ¥20 我的这个coe文件到底哪儿出问题了
  • ¥15 matlab使用自定义函数时一直报错输入参数过多
  • ¥15 设计一个温度闭环控制系统