confusion about friend declarator

Discussion in 'C++' started by Mark, Jan 24, 2005.

  1. Mark

    Mark Guest

    the following is an example of code that won't compile.
    (I've reproduced the problem in a simplistic format )

    ....
    #include <iostream>
    using namespace std;

    class MyClass
    {
    public:
    unsigned int myint;

    ostream& operator<<( ostream& o,MyClass& myclass)
    {
    return o;
    }

    };

    I get : error C2804: binary 'operator <<' has too many parameters

    However I can fix this if I declare it as

    friend ostream& operator<<( ostream& o,MyClass& myclass)
    {
    return o;
    }

    Why? The function is doing nothing that requires access to the internals
    of the class ( there are no internals !! )
     
    Mark, Jan 24, 2005
    #1
    1. Advertising

  2. Mark wrote:
    > the following is an example of code that won't compile.
    > (I've reproduced the problem in a simplistic format )
    >
    > ...
    > #include <iostream>
    > using namespace std;
    >
    > class MyClass
    > {
    > public:
    > unsigned int myint;
    >
    > ostream& operator<<( ostream& o,MyClass& myclass)
    > {
    > return o;
    > }
    >
    > };
    >
    > I get : error C2804: binary 'operator <<' has too many parameters
    >
    > However I can fix this if I declare it as
    >
    > friend ostream& operator<<( ostream& o,MyClass& myclass)
    > {
    > return o;
    > }
    >
    > Why? The function is doing nothing that requires access to the
    > internals of the class ( there are no internals !! )


    The sedond declaration declares a non-member function in the enclosing scope.

    Jonathan
     
    Jonathan Turkanis, Jan 24, 2005
    #2
    1. Advertising

  3. Mark

    Rolf Magnus Guest

    Mark wrote:

    > the following is an example of code that won't compile.
    > (I've reproduced the problem in a simplistic format )
    >
    > ...
    > #include <iostream>
    > using namespace std;
    >
    > class MyClass
    > {
    > public:
    > unsigned int myint;
    >
    > ostream& operator<<( ostream& o,MyClass& myclass)


    Here you define it as member operator with two parameters. But the member
    operator must only have one. It takes the this-object as left-hand
    argument.

    > {
    > return o;
    > }
    >
    > };
    >
    > I get : error C2804: binary 'operator <<' has too many parameters
    >
    > However I can fix this if I declare it as
    >
    > friend ostream& operator<<( ostream& o,MyClass& myclass)
    > {
    > return o;
    > }
    >
    > Why ?


    Now it's a non-member operator, which needs two parameters. That one is just
    a shorter way to write:

    class MyClass
    {
    //...
    friend ostream& operator<<( ostream& o,MyClass& myclass);
    };

    ostream& operator<<( ostream& o,MyClass& myclass)
    {
    return o;
    }


    > The function is doing nothing that requires access to the internals
    > of the class ( there are no internals !! )


    For the declaration of a function, it doesn't matter what that function
    does.
     
    Rolf Magnus, Jan 25, 2005
    #3
    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. Vasileios
    Replies:
    4
    Views:
    490
    Rolf Magnus
    Nov 4, 2003
  2. Gil
    Replies:
    0
    Views:
    467
  3. Per Johansson
    Replies:
    3
    Views:
    1,330
    Mike Wahler
    Nov 7, 2004
  4. xxx
    Replies:
    7
    Views:
    887
  5. Andreas Leitner

    declarator name equals type name weirdness

    Andreas Leitner, Dec 6, 2004, in forum: C Programming
    Replies:
    3
    Views:
    946
    Lawrence Kirby
    Dec 6, 2004
Loading...

Share This Page