I am trying to overload << for a class complex. The code is:
INTERFACE:
class complex
{
friend ostream& operator<<(ostream& output, const complex& V);
private:
double REAL;
double IMG;
public:
complex();
~complex();
};
IMPLEMENTATION of <<:
ostream& operator<<(ostream& output, const complex& V)
{
output<<"("<<V.REAL<< "," << V.IMG<<")";
return output; // for multiple << operators.
}
MS VISUAL C++ 6.0 reports: "error C2248: 'REAL' : cannot access private member declared in class 'complex'."
My understanding is that, << is already defined as a friend
function of the class complex, it should be able to access
the private data REAL and IMG. Why the compiler(I am using Visual
C++ 6.0) says I cant?
Thanks so much.