#include
using namespace std;
class Complex
{
public:
Complex(double i = 0.0, double j = 0.0) :real(i), imag(j){}
friend Complex operator+ (const Complex &c1, const Complex &c2);
friend Complex operator- (const Complex &c1, const Complex &c2);
friend ostream & operator<< (ostream &cout, const Complex &c);
private:
double real;
double imag;
};
Complex operator+ (const Complex &c1, const Complex &c2)
{
return Complex(c1.real + c2.real, c1.imag + c2.imag);
}
Complex operator- (const Complex &c1, const Complex &c2)
{
return Complex(c1.real - c2.real, c1.imag - c2.imag);
}
ostream & operator<< (ostream &cout, Complex &c)
{
cout << c.real << "," << c.imag;
return cout;
}
int main()
{
Complex c1(5, 4), c2(2, 10), c3;
cout << "c1 = " << c1 << endl;
cout << "c2 = " << c2 << endl;
cout << "c3 = " << c3 << endl;
c3 = c1 + c2;
cout << "c3 = " << c3 << endl;
return 0;
}
然后调试一直通不过,说是不能访问私有成员,但是那不是友元函数么?怎么就不能了!!!
求大神解答……