子类模板错误(C ++):“错误:'root'未在此范围内声明”

时间:2014-02-07 19:35:49

标签: c++

我正在研究一个名为“BinarySearchTree”的子类,它继承自超类“BinaryTree”

BinaryTree.h

template <class T>
class BinaryTree 
{

 public:
  BinaryTree() { root = NULL; }

 protected:
  BTNode<T> *root;  // Root node (NULL if the tree is empty)
};

BinarySearchTree.h

template <class T>
class BinarySearchTree : public BinaryTree<T> 
{
 public:
  BinarySearchTree();  // call super's

  bool insert( const T& elem );
  bool insertHelper(BTNode<T> *&, T );
};

template<class T>
BinarySearchTree<T>::BinarySearchTree() :  BinaryTree<T>()
{
    root = NULL; // <---- error: In constructor 'BinarySearchTree<T>::BinarySearchTree()' 
                             //: BinarySearchTree.h:85: error: ‘root’ was not declared in this scope
};

我知道这与根目录中未被识别的根有关,但我该如何解决这个问题呢?

1 个答案:

答案 0 :(得分:1)

例如:

BinaryTree<T>::root = NULL;

请参阅此处获取解释:

http://www.parashift.com/c++-faq/nondependent-name-lookup-members.html