无法访问派生类 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);
}
我尝试将根公开以查看会发生什么,但仍然遇到相同的错误。
任何帮助将不胜感激!
I have a Binary Search Tree as a derived class from a Binary Tree, right now I am trying to access the root for my recursive functions (which is in the base class). But for some reason I keep getting the error:
binSTree.h:31: error: ‘root’ was not declared in this scope
Here are my class declarations:
base class:
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;
};
header file (to line 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);
}
I tried making the root public to see what would happen, and I still got the same error.
any help would be much appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不知道这是为什么,但是当从模板类进行子类化时,为了访问成员,您需要在它们前面加上基类名称前缀。
我的编译器 Visual C++ 不需要这个,但标准出于某种原因需要这样做。或者,您可以将行:
放在任何需要它的范围内。
编辑: 我被 Heavyd 告知你可以使用这个:
I don't know why this is, but when sub-classing from template classes, in order to access members, you need to prefix them with the base class name.
My compiler, Visual C++, doesn't require this, but the standard does for some reason. Alternatively, you can put the line:
in any scope that needs it.
Edit: I was informed by heavyd that you can just use this:
如果没有完整的代码,很难说清楚,但值得注意的是,类模板通常不会像这里那样将代码与声明分开,这也是非模板类的典型情况。
我会将类模板代码移动到您的头文件中(因为这可能是您希望它继续前进的方式)并查看结果是什么。如果您仍然遇到问题,请发布修改后的代码以及对错误消息的任何相关更新。
Without the full code it's hard to tell, but it's worth noting that class templates typically do not separate code from declarations as you have here, and as is typical with non-template classes.
I would move the class template code into your header files (since this is the way you will likely want it going forward) and see what the outcome is. If you still have problems post the modified code with any pertinent updates to the error message(s).