简单的重载,怎么这么多错误。。。可是我一个也没找到啊
程序代码:
#include<iostream> using namespace std; class Complex { private: double real; double image; public: Complex(); //无参构造函数 Complex(double r ); //转换构造函数 Complex(double r, double i);//有两个形参的构造函数 friend Complex operator+(Complex&a, Complex&b); //对"+"运算符进行重载 friend ostream& operator<<(ostream&output, Complex&c); //对"<<"运算符进行重载 friend istream& operator>>(istream&input, Complex&c); //对">>"运算符进行重载 }; Complex::Complex() { real=0; image=0; } Complex::Complex(double r) { real=r; image=0; } Complex::Complex(double r,double i) { real=r; image=i; } Complex operator+(Complex&a,Complex&b) { return Complex(a.real+b.real,a.image+b.image); } ostream & operator<<(ostream output,Complex& c) { output<<c.real<<','<<c.image<<'i'<<endl; return output; } istream & operator>>(istream input,Complex& c) { input>>c.real>>','>>c.image; return input; } int main() { Complex c1; Complex c2; Complex c3; cin>>c1; cin>>c2; c3=c1+c2; cout<<c3; }