Derived classes using "parent's" nested class

Hey,
I've had this problem before with linked list and never really managed to solve it.
I have a generic Tree class which has a nested Node class. For my assignment Node must be nested.
I have a SearchTree class which inherits from Tree, but yet every time I try using a Node in SearchTree it says that Node is undefined. Any idea how to solve this?
Thanks

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
  #pragma once
//-----------------------------------
 // class Tree (Binary Trees)
 // process nodes in Pre/In/Post order
 //-----------------------------------

using namespace std;

 template <class T> class Tree
 {
 protected:
	 //--------------------------------------------------------
		 // inner class Node
		 // a single Node from a binary tree
		 //--------------------------------------------------------
		 #pragma once
		 class Node
		 {
		 public:
			 Node * left;
			 Node * right;
			 T value;
			 Node(T val)
				 : value(val), left(nullptr), right(nullptr){}
			 Node(T val, Node * l, Node * r)
				 : value(val), left(l), right(r){}
			 }; //end of Node class

	 Node * root;
	
 public:
	 Tree() { root = nullptr; } // initialize tree
	 ~Tree();
	 bool isEmpty() const;
	 void clear() { clear(root); root = nullptr; }
	 void preOrder() { preOrder(root); }
	 void inOrder() { inOrder(root); }
	 void postOrder() { postOrder(root); }
	 virtual void process(T val) { cout << val << " "; }
	 virtual void add(T val) = 0;
	 virtual bool search(T val) = 0;
	 virtual void remove(T val) = 0;
 private:
	 void clear(Node * current);
	 void preOrder(Node * current);
	 void inOrder(Node * current);
	 void postOrder(Node * current);
	 };


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
#include "Tree.h"

using namespace std;
//----------------------------------------------------------
// class Tree implementation
 //----------------------------------------------------------
 template <class T>
 Tree<T>::~Tree() // deallocate tree
 {
	if (root != nullptr)
		clear(root);
}
template <class T>
 void Tree<T>::clear(Node * current)
 {
	 if (current)
		 { // Release memory associated with children
		 if (current->left)
			 clear(current->left);
		 if (current->right)
			 clear(current->right);
		 delete current;
		 }
	 }
 template <class T>
 bool Tree<T>::isEmpty() const
 {
	 return root == nullptr;
	 }

 // preOrder processing of tree rooted at current
 template <class T>
 void Tree<T>::preOrder(Node * current)
 { // visit Node, left child, right child
	 if (current)
		 { // process current Node
		 process(current->value);
		 // then visit children
			 preOrder(current->left);
		 preOrder(current->right);
		 }
	 }
 // inOrder processing of tree rooted at current
 template <class T>
 void Tree<T>::inOrder(Node * current)
 { // visit left child, Node, right child
	 if (current)
		 {
		 inOrder(current->left);
		 process(current->value);
		 inOrder(current->right);
		 }
	 }
 // postOrder processing of tree rooted at current
 template <class T>
 void Tree<T>::postOrder(Node * current)
 { // visit left child, right child, node
	 if (current)
		 {
		 postOrder(current->left);
		 postOrder(current->right);
		 process(current->value);
		 }
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#pragma once
#include "Tree.h"

using namespace std;

 template <class T>
 class SearchTree : public Tree<T>
 {
 public:
	 // protocol for search trees
	 void add(T value);
	 bool search(T value)
		 {
		 return search(root, value);
		 }
	 void remove(T value);
 private:
	 void add(Node * current, T val);
	 bool search(Node* current, T val);
	 };


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
#include "SearchTree.h"

using namespace std;

template <class T>
 void SearchTree<T>::add(T val)
 {
	 // add value to binary search tree
		 if (!root)
		 {
		 root = new Node(val);
		 return;
		 }
	 add(root, val);
	 }
 template <class T>
 bool SearchTree<T>::
search(Node * current, T val)
 {
	 // see if argument value occurs in tree
		 if (!current)
		 return false; // not found
	 if (current->value == val)
		 return true;
	 if (current->value < val)
		 return search(current->right, val);
	 else
		 return search(current->left, val);
	 }
template <class T>
 void SearchTree<T>::
add(Node* current, T val)
 {
	 if (current->value < = val)
		 // add to right subtree
		 if (!current->right)
		 {
		 current->right = new Node(val);
		 return;
		 }
	 else add(current->right, val);
	 else
		 // add to left subtree
		 if (!current->left)
		 {
		 current->left = new Node(val);
		 return;
		 }
	 else add(current->left, val);
	 }
 template <class T>
 void SearchTree<T>::remove(T val)
 {

	}
Add this to SearchTree:

 
    using Node = typename Tree<T>::Node;
Last edited on
Thanks!
Topic archived. No new replies allowed.