我有一個二進制搜索樹作爲二叉樹的派生類,現在我試圖訪問我的遞歸函數(這是在基類中)的根。但由於某些原因,我不斷收到錯誤:無法訪問派生類中的保護變量C++
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);
}
我試圖讓公衆知道根本會發生什麼,而且我仍然遇到同樣的錯誤。
任何幫助將不勝感激!
我們可以看到整個(至少到第31行)binSTree.h的嗎? – 2010-10-22 02:40:00
當然!我可以做 – rajh2504 2010-10-22 02:45:04
如果您想了解爲什麼會發生這種情況...... http://www.parashift.com/c++-faq-lite/templates.html#faq-35.19 – 2010-10-22 04:13:30