Error define ostream operator of private nested class

Discussion in 'C++' started by kurt krueckeberg, Oct 28, 2012.

  1. Can't you define an ostream operator of a private nested class outside the nested class? This compiles successfully on gcc 4.6.3

    class FlightMap {
    // snip
    class Node {
    public:
    Node(int id, bool b);
    Node(const Node&);
    int cityId;
    bool visited;
    friend ostream& operator<<(ostream& o, const Node& n)
    {
    o << "Node(int, bool) = " << "(" << n.cityId << ". )" << n.visited;
    return o;
    }
    };
    // snip
    };

    but if I define the ostream operator outside the class
    class FlightMap {

    class Node {
    public:
    // snip
    friend ostream& operator<<(ostream& o, const Node& n);
    };
    // snip
    };

    ostream& operator<<(ostream& o, const FlightMap::Node& n)
    {
    o << "Node(int, bool) = " << "(" << n.cityId << ". )" << n.visited;
    return o;
    }

    I get an error about FlightMap::Node being a private class:

    src/stack-directedpath.cpp: In function ‘std::eek:stream& operator<<(std::eek:stream&, const FlightMap::Node&)’:
    .../src/stack-directedpath.cpp:22:10: error: ‘class FlightMap::Node’ is private
    .../src/stack-directedpath.cpp:45:57: error: within this context


    Why does it matter that FlightMap::Node is private? I'm not declaring an instance of FlightMap::Node. I'm just trying to define the ostream operator?
    kurt krueckeberg, Oct 28, 2012
    #1
    1. Advertising

  2. kurt krueckeberg

    SG Guest

    Am 28.10.2012 15:59, schrieb kurt krueckeberg:

    > [...]
    > but if I define the ostream operator outside the class
    > class FlightMap {
    > class Node {
    > public:
    > // snip
    > friend ostream& operator<<(ostream& o, const Node& n);
    > };
    > // snip
    > };
    >
    > ostream& operator<<(ostream& o, const FlightMap::Node& n)
    > {
    > o << "Node(int, bool) = " << "(" << n.cityId << ". )" << n.visited;
    > return o;
    > }
    >
    > I get an error about FlightMap::Node being a private class:
    >
    > src/stack-directedpath.cpp: In function ‘std::eek:stream& operator<<(std::eek:stream&, const FlightMap::Node&)’:
    > ../src/stack-directedpath.cpp:22:10: error: ‘class FlightMap::Node’ is private
    > ../src/stack-directedpath.cpp:45:57: error: within this context
    >
    > Why does it matter that FlightMap::Node is private? I'm not declaring an instance of FlightMap::Node. I'm just trying to define the ostream operator?


    You're not declaring an instance. But you're still referring to the type
    FlightMap::Node -- A type that is private. Try making your operator a
    friend of FlightMap as well.

    Alternativly: You could just as well define Node outside of FlightMap,
    perhaps in a special namespace.

    Cheers!
    SG
    SG, Oct 28, 2012
    #2
    1. Advertising

  3. On 10/28/2012 10:59 AM, kurt krueckeberg wrote:
    > Can't you define an ostream operator of a private nested class outside the nested class? This compiles successfully on gcc 4.6.3
    >
    > class FlightMap {
    > // snip
    > class Node {
    > public:
    > Node(int id, bool b);
    > Node(const Node&);
    > int cityId;
    > bool visited;
    > friend ostream& operator<<(ostream& o, const Node& n)
    > {
    > o << "Node(int, bool) = " << "(" << n.cityId << ". )" << n.visited;
    > return o;
    > }
    > };
    > // snip
    > };
    >
    > but if I define the ostream operator outside the class
    > class FlightMap {
    >
    > class Node {
    > public:
    > // snip
    > friend ostream& operator<<(ostream& o, const Node& n);
    > };
    > // snip
    > };
    >
    > ostream& operator<<(ostream& o, const FlightMap::Node& n)
    > {
    > o << "Node(int, bool) = " << "(" << n.cityId << ". )" << n.visited;
    > return o;
    > }
    >
    > I get an error about FlightMap::Node being a private class:
    >
    > src/stack-directedpath.cpp: In function ‘std::eek:stream& operator<<(std::eek:stream&, const FlightMap::Node&)’:
    > ../src/stack-directedpath.cpp:22:10: error: ‘class FlightMap::Node’ is private
    > ../src/stack-directedpath.cpp:45:57: error: within this context
    >
    >
    > Why does it matter that FlightMap::Node is private? I'm not declaring an instance of FlightMap::Node. I'm just trying to define the ostream operator?
    >


    The operator<< defined in the namespace does not have the access to
    private members of 'FlightMap' (like 'Node'), and hence the error. When
    op<< is defined inside 'Node' itself, there is no need to look in
    'FlightMap' class to resolve 'Node'.

    V
    --
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Oct 28, 2012
    #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. E11
    Replies:
    1
    Views:
    4,698
    Thomas Weidenfeller
    Oct 12, 2005
  2. Tim Partridge
    Replies:
    1
    Views:
    696
    Sumit Rajan
    Jun 2, 2004
  3. DaveLessnau
    Replies:
    3
    Views:
    412
    Howard
    May 16, 2005
  4. Rock
    Replies:
    4
    Views:
    420
    Jim Langston
    Nov 20, 2005
  5. Replies:
    2
    Views:
    1,881
Loading...

Share This Page