making ostream& operator<< a friend function of a class in a namespace

Discussion in 'C++' started by Tim Partridge, Jun 2, 2004.

  1. I want operator<< to be a friend function of a class inside a namespace, but
    I don't know how. For example:

    #include <iostream>

    namespace ns {
    class foo {
    public:
    // there is something wrong with the line below, but I don't
    know how to fix it. I think I need to
    // specify the namespace and class for the operator<<, but if I
    write std::eek:stream::eek:perator<<
    // i get all sorts of errors I don't understand.
    friend std::eek:stream& operator<<( std::eek:stream &stream, const foo
    &f );
    private:
    int i;
    };
    }

    std::eek:stream& operator<<( std::eek:stream &stream, const ns::foo &f ) {
    stream << f.i; // compiler complains i is private
    return stream;
    }

    int main(int argc, char* argv[]) {
    return 0;
    }
     
    Tim Partridge, Jun 2, 2004
    #1
    1. Advertising

  2. Tim Partridge

    Sumit Rajan Guest

    "Tim Partridge" <> wrote in message
    news:ptnvc.326514$...
    > I want operator<< to be a friend function of a class inside a namespace,

    but
    > I don't know how. For example:
    >
    > #include <iostream>
    >
    > namespace ns {
    > class foo {
    > public:
    > // there is something wrong with the line below, but I don't
    > know how to fix it. I think I need to
    > // specify the namespace and class for the operator<<, but if

    I
    > write std::eek:stream::eek:perator<<
    > // i get all sorts of errors I don't understand.
    > friend std::eek:stream& operator<<( std::eek:stream &stream, const

    foo
    > &f );
    > private:
    > int i;
    > };
    > }
    >
    > std::eek:stream& operator<<( std::eek:stream &stream, const ns::foo &f ) {
    > stream << f.i; // compiler complains i is private
    > return stream;
    > }
    >
    > int main(int argc, char* argv[]) {
    > return 0;
    > }
    >



    #include <iostream>

    namespace ns {
    class foo {
    public:
    foo(int t=45):i(t){}
    friend std::eek:stream& operator<<( std::eek:stream &stream, const
    foo& f );
    private:
    int i;
    };
    std::eek:stream& operator << ( std::eek:stream &stream, const ns::foo &f );
    // or you could define the operator right here
    }

    std::eek:stream& ns::eek:perator<<( std::eek:stream &stream, const ns::foo &f ) {
    stream << f.i;
    return stream;
    }

    int main(int argc, char* argv[]) {
    ns::foo f(4);
    std::cout << f << '\n';
    }
     
    Sumit Rajan, Jun 2, 2004
    #2
    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. Rock
    Replies:
    4
    Views:
    437
    Jim Langston
    Nov 20, 2005
  2. truce
    Replies:
    5
    Views:
    653
    truce
    Jan 14, 2006
  3. Layton
    Replies:
    2
    Views:
    459
    Layton
    Sep 27, 2006
  4. Replies:
    2
    Views:
    1,911
  5. Peter
    Replies:
    2
    Views:
    278
    Öö Tiib
    Jun 6, 2013
Loading...

Share This Page