R
Robert Wierschke
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
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