Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Error define ostream operator of private nested class

Reply
Thread Tools

Error define ostream operator of private nested class

 
 
kurt krueckeberg
Guest
Posts: n/a
 
      10-28-2012
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?
 
Reply With Quote
 
 
 
 
SG
Guest
Posts: n/a
 
      10-28-2012
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: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?


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

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      10-28-2012
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: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?
>


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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Strange compiler error w.r.t 'friend ostream& operator<< <>(ostream&,...)' abhay.burli@gmail.com C++ 2 02-26-2009 06:18 AM
friend ostream& operator <<, compiles and runs in Dev++ no output or error in Borland Rock C++ 4 11-20-2005 06:26 AM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
Public Data in Private Class or Private Data in Public Class? DaveLessnau C++ 3 05-16-2005 06:53 PM
making ostream& operator<< a friend function of a class in a namespace Tim Partridge C++ 1 06-02-2004 05:07 PM



Advertisments