子类模板错误 (C++): "error: 'root' was not declared in this scope"

Subclass Template error (C++): "error: 'root' was not declared in this scope"

本文关键字:root not scope this in declared was error 错误 C++ 子类      更新时间:2023-10-16

>我正在研究一个名为"BinarySearchTree"的子类,它继承自其超类"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
};

我知道这与根在范围内无法识别有关,但是我该如何解决此问题?

例如:

BinaryTree<T>::root = NULL;

有关解释,请参阅此处:

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