overloading operator<< as global friend funcion

Discussion in 'C++' started by Robert Wierschke, Jul 31, 2004.

  1. 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
    Robert Wierschke, Jul 31, 2004
    #1
    1. Advertising

  2. Robert Wierschke

    Sharad Kala Guest

    "Robert Wierschke" <> wrote in message
    news:ceg6l6$vmo$04$-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
    Sharad Kala, Jul 31, 2004
    #2
    1. Advertising

  3. On Sat, 31 Jul 2004 15:27:30 +0200, Robert Wierschke <> 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
    John Harrison, Jul 31, 2004
    #3
  4. Robert Wierschke

    Louise56 Guest

    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
    Louise56, Aug 3, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    378
    Alvin Bruney - ASP.NET MVP
    Sep 28, 2005
  2. Kay
    Replies:
    1
    Views:
    385
    John Harrison
    Sep 3, 2004
  3. Replies:
    4
    Views:
    520
    Greg Comeau
    Sep 30, 2006
  4. Eric Lilja
    Replies:
    7
    Views:
    335
    Victor Bazarov
    Nov 9, 2006
  5. Replies:
    6
    Views:
    669
    James Kanze
    May 15, 2009
Loading...

Share This Page