Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > overloading operator<< as global friend funcion

Reply
Thread Tools

overloading operator<< as global friend funcion

 
 
Robert Wierschke
Guest
Posts: n/a
 
      07-31-2004
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








 
Reply With Quote
 
 
 
 
Sharad Kala
Guest
Posts: n/a
 
      07-31-2004

"Robert Wierschke" <(E-Mail Removed)> wrote in message
news:ceg6l6$vmo$04$(E-Mail Removed)-online.com...
> 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


 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      07-31-2004
On Sat, 31 Jul 2004 15:27:30 +0200, Robert Wierschke <(E-Mail Removed)> wrote:

> 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
 
Reply With Quote
 
Louise56
Guest
Posts: n/a
 
      08-03-2004
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help me with callback funcion. Solang C++ 2 09-24-2009 06:53 PM
Why might you define an abstract member funcion? Pete C C++ 8 10-09-2005 08:25 PM
How to display data in drilldown funcion ? kelvinweb@gmail.com ASP .Net 1 09-28-2005 01:09 PM
Linked list inserting items from two different funcion Kay C++ 1 09-03-2004 03:48 PM
Newbie Question: funcion containing this.id not reading Jeremy Javascript 1 07-07-2004 06:08 PM



Advertisments