我有一个二叉搜索树作为二叉树的派生类,现在我正在尝试访问我的递归函数(在基类中)的根。但由于某种原因,我不断收到错误:
binSTree.h:31: error: ‘root’ was not declared in this scope
这是我的班级声明:
基类:
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;
};
头文件(到第 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);
}
我尝试公开根目录以查看会发生什么,但我仍然遇到同样的错误。
任何帮助将非常感激!