Читал книги. Видел много однотипных примеров. Везде написано делать так:
#include <iostream>
using namespace std;
class Complex
{
double re;
double im;
public:
Complex (double re, double im);
friend ostream& operator<< (ostream& s, const Complex& z);
};
Complex::Complex (double re, double im)
{
this->re = re;
this->im = im;
}
ostream& operator<< (ostream& s, Complex& z)
{
return s << '(' << z.re << ',' << z.im << ')';
}
void main(void)
{
Complex a(1.2,0.2);
cout << "a=" << a;
}
Однако,
Z:\Coding\Cpp\iotest\main.cpp(23) : error C2248: 're' : cannot access private member declared in class 'Complex'
Z:\Coding\Cpp\iotest\main.cpp(6) : see declaration of 're'
Z:\Coding\Cpp\iotest\main.cpp(23) : error C2248: 'im' : cannot access private member declared in class 'Complex'
Z:\Coding\Cpp\iotest\main.cpp(7) : see declaration of 'im'
Почему? Ведь friend же??