overloading operator<< as global friend funcion

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
 
S

Sharad Kala

Robert Wierschke said:
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;

The above semicolon is not required.

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

}
;
A semicolon is required here.
The operator function is defined as

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

Seems like problem with VC 6. It compiles fine with VC 7.0

-Sharad
 
J

John Harrison

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

It's a known bug in VC++ 6. I believe you can fix it by downloading the
latest service pack (SP5 I think). Also I think you can fix it by defining
the function in the class, i.e.

class Vector {
...
friend ostream & operator<<(ostream & os, const Vector& v)
{
// code here
}
};

However you should consider upgrading to a beter compiler than VC++ 6.
It's along way from being a correct implementation of the C++ language.

You could consider VC++ 7.1 or gcc 3.3, both are free.

john
 
L

Louise56

I was having the same problem tonight. What I had to do was to make a
forward declaration of the friend function.

//--Forward declaration
ostream & operator<<( ostream & os, const Vector& v);

//--Now define your class
class Vector
{
-------
-------

public:
friend ostream & operator << ( ostream & os,
const Vector& v );
}; //--End of class Vector

//--Now define your operator function, which can be put
//--at the end of your class declaration in the
//--at the header file or in the .cpp file.

ostream & operator<<( ostream & os, const Vector& v)
{
----------------;
}


Louise
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top