Error define ostream operator of private nested class


K

kurt krueckeberg

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?
 
Ad

Advertisements

S

SG

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
 
Ad

Advertisements

V

Victor Bazarov

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top