namespace problems with friend function

Discussion in 'C++' started by Tim Partridge, Jan 7, 2004.

  1. I'm not very good with namespaces, so my problem is probably a simple one.
    I can't get the following to compile on gcc 3.3.1. It reports

    main.cc: In function 'std::eek:stream & operator<<(std::eek:stream, const
    ns::C&)':
    main.cc:12: error: 'int ns::C::i' is private
    main.cc:17: error: within this context
    main.cc: In function 'int main(int, char**)':
    main.cc:23: error: ambiguous overload for 'operator<<' in 'std::cout << c'
    main.cc:16: error: candidates are: std::eek:stream& operator<<(std::eek:stream&,
    const ns::C&)
    main.cc:9: error: std::eek:stream& ns::eek:perator<<(std::eek:stream&, const
    ns::C&)
    make: *** [main.o] Error 1

    #include <iostream>

    namespace ns {
    class C {
    public:
    C(int i) : i(i) {};
    ~C() {};

    friend std::eek:stream &operator<<(std::eek:stream
    &stream, const C &c); // line 9
    private:
    int i; // line 12
    };
    }

    std::eek:stream &operator<<(std::eek:stream &stream, const ns::C &c) {
    stream << "C(" << c.i << ")" << std::endl; // line 17
    return stream;
    }

    int main(int argc, char *argv[]) {
    ns::C c(4);
    std::cout << c; // line 23
    return 0;
    }
    Tim Partridge, Jan 7, 2004
    #1
    1. Advertising

  2. "Tim Partridge" <> wrote in message
    news:p...
    > I'm not very good with namespaces, so my problem is probably a simple one.
    > I can't get the following to compile on gcc 3.3.1. It reports
    >
    > main.cc: In function 'std::eek:stream & operator<<(std::eek:stream, const
    > ns::C&)':
    > main.cc:12: error: 'int ns::C::i' is private
    > main.cc:17: error: within this context
    > main.cc: In function 'int main(int, char**)':
    > main.cc:23: error: ambiguous overload for 'operator<<' in 'std::cout << c'
    > main.cc:16: error: candidates are: std::eek:stream& operator<<(std::eek:stream&,
    > const ns::C&)
    > main.cc:9: error: std::eek:stream& ns::eek:perator<<(std::eek:stream&, const
    > ns::C&)
    > make: *** [main.o] Error 1
    >
    > #include <iostream>
    >
    > namespace ns {
    > class C {
    > public:
    > C(int i) : i(i) {};
    > ~C() {};
    >
    > friend std::eek:stream &operator<<(std::eek:stream
    > &stream, const C &c); // line 9
    > private:
    > int i; // line 12
    > };
    > }
    >
    > std::eek:stream &operator<<(std::eek:stream &stream, const ns::C &c) {
    > stream << "C(" << c.i << ")" << std::endl; // line 17
    > return stream;
    > }
    >
    > int main(int argc, char *argv[]) {
    > ns::C c(4);
    > std::cout << c; // line 23
    > return 0;
    > }


    Put the implementation of the friend function in the same namespace...
    something like...

    #include <iostream>

    namespace ns {
    class C {
    public:
    C(int i) : i(i) {}

    friend std::eek:stream &operator << (std::eek:stream &, const C &);
    private:
    int i;
    };
    }

    namespace ns {
    std::eek:stream &operator << (std::eek:stream &o, const C &c) {
    o << "C (" << c.i << ")" << std::endl;
    return o;
    }
    }

    int main (int argc, char *argv[]) {
    ns::C c (4);
    std::cout << c;
    }

    // C (4)

    Regards

    Brian
    Brian MacBride, Jan 7, 2004
    #2
    1. Advertising

  3. "Tim Partridge" <> wrote in message
    news:p...
    > I'm not very good with namespaces, so my problem is probably a simple one.
    > I can't get the following to compile on gcc 3.3.1. It reports
    >
    > main.cc: In function 'std::eek:stream & operator<<(std::eek:stream, const
    > ns::C&)':
    > main.cc:12: error: 'int ns::C::i' is private
    > main.cc:17: error: within this context
    > main.cc: In function 'int main(int, char**)':
    > main.cc:23: error: ambiguous overload for 'operator<<' in 'std::cout << c'
    > main.cc:16: error: candidates are: std::eek:stream& operator<<(std::eek:stream&,
    > const ns::C&)
    > main.cc:9: error: std::eek:stream& ns::eek:perator<<(std::eek:stream&, const

    NB ns::eek:perator<<
    > ns::C&)
    > make: *** [main.o] Error 1
    >
    > #include <iostream>
    >
    > namespace ns {
    > class C {
    > public:
    > C(int i) : i(i) {};
    > ~C() {};
    >
    > friend std::eek:stream &operator<<(std::eek:stream
    > &stream, const C &c); // line 9


    This says you have a friend called
    ns::eek:perator<<(std::eek:stream&,const ns::C&)

    > private:
    > int i; // line 12
    > };
    > }
    >
    > std::eek:stream &operator<<(std::eek:stream &stream, const ns::C &c) {
    > stream << "C(" << c.i << ")" << std::endl; // line 17
    > return stream;
    > }


    This declares a (non-friend) global called
    ::eek:perator<<(std::eek:stream&,const ns::C&)

    >
    > int main(int argc, char *argv[]) {
    > ns::C c(4);
    > std::cout << c; // line 23


    both ::eek:perator<< and ns::eek:perator<< match hence ambiguity

    > return 0;
    > }


    Personally I find it clearer never to use friends - declare a public print
    method
    and declare a non-friend operator<< to use it.
    Nick Hounsome, Jan 8, 2004
    #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. Alexander Grigoriev
    Replies:
    6
    Views:
    2,542
    Kevin Goodsell
    Sep 12, 2003
  2. Tim Partridge
    Replies:
    1
    Views:
    697
    Sumit Rajan
    Jun 2, 2004
  3. Layton
    Replies:
    2
    Views:
    447
    Layton
    Sep 27, 2006
  4. Noah Roberts
    Replies:
    2
    Views:
    438
    Noah Roberts
    Mar 22, 2011
  5. Peter
    Replies:
    2
    Views:
    265
    Öö Tiib
    Jun 6, 2013
Loading...

Share This Page