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:stream& operator<<(std: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?
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:stream& operator<<(std: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?