overloading operator<< as global friend funcion

From: Robert Wierschke (wierob_at_gmx.de)
Date: 07/31/04


Date: Sat, 31 Jul 2004 15:27:30 +0200

Hi

I want to overload the operator<< for a class Vector.

class Vector {
 double x;
 double y;
 double z;

public:
 Vector( double X, double Y, double Z) {
  x = X;
  y = Y;
  z = Z;
};

    friend ostream & operator<<( ostream & os, const Vector& v);

}

The operator function is defined as

ostream & operator<<( ostream & os, const Vector & v)
{
 os << v.x << " " << v.y << " " << v.z << endl;
 return os;
}

in global scope. The function use the private members of the Vector calss.
That's way I declared the function as friend.

The problem is that it doesn't work and the compiler(VC++6) says that the
function cannot access the private members x, y, z.

I'm a newbie but shouldn't the friend keyword solve this problem? Also the
code is equal to samples I've found in books.

The code works fine if I change the function name "operator<<" to some thing
normal e. g. "func". But this is no operator overloading.

thanks for help