重载成员函数只能有一个参数吗?我写的operator-中写一个参数是对的,写了两个参数就是说参数太多了
#include <iostream>
using namespace std;
class complex {
public:
double real, imag;
complex(double r=0.0,double i=0.0):real(r),imag(i){}
complex operator-(complex& a,complex &e);
};
complex operator+(complex& b, complex& c) {
return complex(b.real + c.real, b.imag + c.imag);
}
complex complex::operator-(complex& a,complex &e) {
return complex(real - a.real+e.real, imag - a.imag+e.imag);
}
int main() {
complex a(4.0, 6), b(5, 9), c,e(1,2);
c = a - b-e;
cout << c.real << "," << c.imag << endl;
cout << (a + b).real << "," << (a + b).imag << endl;
}