friend ostream& operator<< ....

Hi, it's probably something small, but I can't seem to find it. So I have a friend set up in my declarations.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 private:

    struct Node
    {
      int Element;   // User data item
      Node * Next;   // Link to the node's successor
    };

    unsigned Count;  // Number of user data items in the set
    Node * Head;     // Link to the head of the chain

 public:

friend ostream& operator<<( ostream&, const Set& );


And heres the function from my .cpp file.
1
2
3
4
5
6
7
8
9
10
11
12
13
ostream& operator<<( ostream& Out, const Set& A)
{
	unsigned Position =0;
	cout << endl << "{ ";
	Node * Temp = A.Head->Next;
	while (Position<A.Count)
	{
		cout << Temp->Next << ", ";
	}
	cout << " }" << endl;
	
	return Out;
}


And I get this error,
1
2
3
proj08.set.cpp: In function 'std::ostream& operator<<(std::ostream&, const Set&)':
proj08.set.cpp:162: error: 'Node' was not declared in this scope
proj08.set.cpp:162: error: 'Temp' was not declared in this scope


I dont know why it's not declared. Shouldn't it be because of the friend statement? Sorry, I haven't done this much.
From what I read about C++, I learned that defining structures inside classes, or classes inside classes isn't within the C++ standard. Please don't do that!!! Define your struct outside your class, and create an object to that in your class. And this whole problem will be solved.
Hmm, I'm not exactly sure what you mean. I can't change the library file. I have to write the class using the friend operator inside the class if that what you're talking about.
I mean that nesting classes is illegal in C++:

1
2
3
4
5
6
7
class MyClass
{
   class MySubClass //totally illegal
   {

   };
};


This is what you're doing... and it's not within the C++ standard. I read that some compilers could accept it, but it still makes problems at some level.
Take your subclass (or struct for this purpose) outside your class, and have an object of your subclass in your class. This should work fine.

Source of info: Professional C++, by Solter and Kleper
No, nested classes are legal. That is not the problem.

When you use the Node class outside the class scope you have to put the name of the surrounding class before Node:

1
2
3
4
5
6
7
8
9
10
11
12
13
ostream& operator<<( ostream& Out, const Set& A)
{
	unsigned Position =0;
	cout << endl << "{ ";
	Set::Node * Temp = A.Head->Next;
	while (Position<A.Count)
	{
		cout << Temp->Next << ", ";
	}
	cout << " }" << endl;
	
	return Out;
}
Ah, wonderful! thank you very much!
@TheDestroyer, I don't know what you want me to read on that page. operator<< is a friend of Set so it can use Node even though it's private.
Topic archived. No new replies allowed.