compiler won't let me add Node struct as parameter

I have a function, void printElementsInNode( Node *node ), which is called everytime the binary search tree traverses from one node to another within void printAll(). The printElementsInNode function is supposed to print the elements in the current node. Unfortunately, the compiler gives me this error: "'Node' has not been declared". Why can't the compiler find my user created struct to use as a parameter?

(Also, I apologize for how much code is in each function. I'm not to the refactoring stage yet).

Here is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#include <iostream>

template <typename Object>
class Set
{
	
         public:
		Set( Object initialValue )
		{
			/*initializes the values of
			the root Node*/
			root = new Node;
			root->parent = nullptr;
			root->left = nullptr;
			root->right = nullptr;
			root->nextPrintOut = 'L';
			root->data = initialValue;
		}
		
		bool search( Object valueToFind )
		{
			std::cout << "Search function entered" << std::endl;
			Node *traversalNode = root;
			bool valueFound = false;

			//iterates through tree until a node 
with no children is reached
			do
			{ 
				std::cout << "do-while loop entered" << std::endl;
				if( traversalNode->data == valueToFind )
				{
					std::cout << "If-condition entered" << std::endl;
					valueFound = true;
					std::cout << "Value (" << valueToFind << ") FOUND!" << std::endl;
					return valueFound;
				}
				/*if value is less than current node, traverse
				to left child*/
				else if( valueToFind < traversalNode->data && traversalNode->left != nullptr )
				{
					std::cout << "Else if entered: value is LESS than current node" << std::endl; 
					traversalNode = traversalNode->left;
				}
				/*if value is more than current node, traverse
				 * to right child*/
				else if( valueToFind > traversalNode->data && traversalNode->right != nullptr )
				{
					std::cout << "Else if entered: value is MORE than current node" << std::endl;
					traversalNode = traversalNode->right;
				}
			} while( traversalNode->left || traversalNode->right != nullptr );
			
			/*if nothing was returned in while-loop then that means that
			the search did not find the desired value*/
			std::cout << "Value (" << valueToFind << ") NOT found" << std::endl;
			return valueFound;

		}

		void insert( Object valueToAdd )
		{
			bool valueFound = search( valueToAdd );
			Node *iteratorNode = root;
			Node *newNode = new Node;

			newNode->data = valueToAdd;
			newNode->nextPrintOut = 'L';
			newNode->parent = nullptr;
			newNode->left = nullptr;
			newNode->right = nullptr;

			if( valueFound == true )
			{
				std::cout << "Value FOUND so will not be added to tree" << std::endl;
			}
			else
			{
				std::cout << "Value NOT found so WILL be added to tree" << std::endl;
				do
				{
					/*traverses the tree until the iteratorNode 
					 * reaches a nullptr. After that, the 
					 * new node is added as a child to the 
					 * current iteratorNode*/
					if( valueToAdd < iteratorNode->data )
					{
						if( iteratorNode->left != nullptr )
						{
							iteratorNode = iteratorNode->left;	
						}
						else
						{
							iteratorNode->left = newNode;
							newNode->parent = iteratorNode;
							iteratorNode = newNode;
						}
					}
					else if( valueToAdd > iteratorNode->data )
					{
						if( iteratorNode->right != nullptr )
						{
							iteratorNode = iteratorNode->right;
						}
						else
						{
							iteratorNode->right = newNode;
							newNode->parent = iteratorNode;
							iteratorNode = newNode;
						}
					}
				} while( iteratorNode->left || iteratorNode->right != nullptr );
			}
		}
		
		void printAll()
		{
			Node *printNode = root;
			
			printElementsInNode( printNode );

			while( root->nextPrintOut != 'P' )
			{
				if( printNode == root )
				{
					std::cout << "Root node; ";
				}

				if( printNode->nextPrintOut == 'L' )
				{
					if( printNode->left != nullptr )
					{
						printNode->nextPrintOut = 'R';
					}
					else
					{
						printNode->nextPrintOut = 'R';
						printNode = printNode->left;
					}
				}
				else if( printNode->nextPrintOut == 'R' )
				{
					if( printNode->right != nullptr )
					{
						printNode->nextPrintOut = 'P';
					}
					else
					{
						printNode->nextPrintOut = 'P';
						printNode = printNode->right;
					}
				}
				else if( printNode->nextPrintOut == 'P' )
				{
					printNode->nextPrintOut = 'L';
					printNode = printNode->parent;
				}
			}
		}

		void printElementsInNode( Node *node )
		{
			std::cout << "Data: " << node->data << "; ";

			if( node->parent == nullptr )
			{
				std::cout << "Parent: nullptr; ";
			}
			else
			{
				std::cout << "Parent: " << node->parent << "; ";
			}

			if( node->left == nullptr )
			{
				std::cout << "Left: nullptr; ";
			}
			else
			{
				std::cout << "Left: " << node->left << "; ";
			}
			if( node->right == nullptr )
			{
				std::cout << "Right: nullptr; ";				
			}
			else
			{
				std::cout << "Right: " << node->right << "; ";
			}

			std::cout << "NextPrintOut: " << node->nextPrintOut;
			std::cout << '\n';

		}

	private:
		struct Node
		{
			Node *parent;
			Node *left;
			Node *right;
			int data;
			/*keeps track of where node needs to point next
			 * in printAll() function:
			 * "L" == Left, "R" == Right, "P" == Parent*/
			char nextPrintOut;
		};

		Node *root;
};


int main()
{
	Set<int> mySet( 5 );
	
	mySet.printAll();

	return 0;
}

Last edited on
The compiler starts at the top and goes down. It needs to know what a Node object is BEFORE it needs to use one.

Your code is declaring what a Node object is AFTER the compiler needs to know what it is.
Option 1:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
template <typename Object>
class Set
{
private:
	struct Node
	{
		Node *parent;
		Node *left;
		Node *right;
		int data;
		char nextPrintOut;
	};

    Node *root;

public:
  // public code
};


I can't believe I didn't think of that. I utilized your example, @keskiverto, and the compiler no longer complained. Thanks to you both.
By the way, should data be of type Object?
Topic archived. No new replies allowed.