views:

62

answers:

2

I have a Binary Search Tree as a derived class from a Binary Tree, right now I am trying to access the root for my recursive functions (which is in the base class). But for some reason I keep getting the error:

binSTree.h:31: error: ‘root’ was not declared in this scope

Here are my class declarations:

base class:

template <class T>
class binTree {
private:

  int height(treeNode<T>*) const;          // Recursive method
  int size(treeNode<T>*) const;            // Recursive method
  int leaves(treeNode<T>*) const;

  void insert(treeNode<T>*&, const T&);

  void clear(treeNode<T>*);
  treeNode<T>* copy_tree(treeNode<T>*);

  void preOrder(treeNode<T>*, void (*)(T&));
  void inOrder(treeNode<T>*, void (*)(T&));
  void postOrder(treeNode<T>*, void (*)(T&));
public:
   binTree();
   binTree(const binTree<T>&);
   ~binTree();

   bool empty() const;

   void clear();

   void insert(const T&);
   int remove(const T&);                 // Extra credit only

   int height() const;                   // Non-recursive method
   int size() const;                     // Non-recursive method
   int leaves() const;

   void preOrder(void (*)(T&));
   void inOrder(void (*)(T&));
   void postOrder(void (*)(T&));

   const binTree<T>& operator=(const binTree<T>&);
protected:
   treeNode<T>* root;
};

header file (to line 31):

#include "binTree.h"

template<class T>
class binSTree : public binTree<T> {
public:
  void insert(const T&);
  bool remove(const T&);
  bool search(const T&, int&) const;
private:
  void insert(treeNode<T>*&, const T&);
  bool remove(treeNode<T>*&, const T&);
  bool search(treeNode<T>*, const T&, int&);
  void remove_root(treeNode<T>*&);
};

template<class T>
void binSTree<T>::insert(const T& x) {
treeNode<T>* newNode = new treeNode<T>(x);
insert(newNode, x);
}

template<class T> // public
bool binSTree<T>::remove(const T& x) {
return remove(binTree<T>.root, x);
}

template<class T> // public
bool binSTree<T>::search(const T& x, int& len) const {
len = 0;
len = search(root,x,len);
}

I tried making the root public to see what would happen, and I still got the same error.

any help would be much appreciated!

A: 

Without the full code it's hard to tell, but it's worth noting that class templates typically do not separate code from declarations as you have here, and as is typical with non-template classes.

I would move the class template code into your header files (since this is the way you will likely want it going forward) and see what the outcome is. If you still have problems post the modified code with any pertinent updates to the error message(s).

Steve Townsend
both of the classes and all of their declarations are already in the header files, if I understand your response correctly.
rajh2504
@Greg - your update to the posted code makes this clear, thanks.
Steve Townsend
+1  A: 

I don't know why this is, but when sub-classing from template classes, in order to access members, you need to prefix them with the base class name.

len = search( binTree<T>::root, x,len);

My compiler, Visual C++, doesn't require this, but the standard does for some reason. Alternatively, you can put the line:

using binTree<T>::root;

in any scope that needs it.

Edit: I was informed by heavyd that you can just use this:

this->root
PigBen
Using `this->root` seems to work for gcc as well.
heavyd
Ahh, so it does. That's much better. I don't use inheritance much, let alone inheritance from template classes, so these things escape me.
PigBen
thank you so much guys this is a big help!
rajh2504
@heavyd can I take this -> root and put it in a temporary node such as:
rajh2504
treeNode<T>* tmp = this -> root;
rajh2504