gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:24:16 25 4
gpt4 key购买 nike

我正在研究一个名为“BinarySearchTree”的子类,该子类继承自其父类(super class)“BinaryTree”

二叉树.h

template <class T>
class BinaryTree
{

public:
BinaryTree() { root = NULL; }

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

二叉搜索树.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

关于c++ - 子类模板错误(C++): "error: ' root' was not declared in this scope",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21636329/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com