开发者

cannot access protected variable in derived class c++

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:

开发者_如何学Gobase 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!


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


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).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜