我正在尝试基于二叉树搜索 实现一个集合。所以我从 root(指向 Node 的指针)开始构建这个集合,其中 Node 有一个值,左右子节点(也都是指向 Node 的指针)。所以这样我就可以通过将 root->right 指向创建的节点等来在根节点的右侧设置一个新节点。看一下定义:
template <class T>
class Set
{
public:
Set();
~Set();
void push(const T&);
bool belongs(const T&) const;
void remove(const T&);
const T& min() const;
const T& max() const;
unsigned int cardinal() const;
void show(std::ostream&) const;
friend ostream& operator<<(ostream& os, const Set<T> &c) {
c.show(os);
return os;
}
private:
struct Node
{
Node(const T& v);
T value;
Node* left;
Node* right;
};
Node* root_;
int cardinal_;
Node & fatherOfNode(const Node & root, const T & key, const bool hook) const;
};
...
// This function is the one with errors.
template <class T>
Node & Set<T>::fatherOfNode(const Node & root, const T & key, const bool hook) const {
// Some code
}
所以我遇到了这个错误:
/home/jscherman/ClionProjects/algo2-t3-bts/set.hpp:247:1: error: ‘Node’ does not name a type
Node & Set<T>::fatherOfNode(const Node & root, const T & key, const bool hook) const {
^
我看过很多与此错误相关的帖子,但其中大部分是由于在函数定义之前编写函数实现引起的。如您所见,fatherOfNode 的实现在其定义之下,因此我的情况似乎并非如此。
知道发生了什么事吗?
最佳答案
Node
是 Set
中的一个内部类,所以在这个类之外你需要解决这个问题:
Set<T>::Node
所以你的函数定义需要是:
template <class T>
typename Set<T>::Node & Set<T>::fatherOfNode(const Set<T>::Node & root, const T & key, const bool hook) const {
关于c++ - 对象没有命名类型 - C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39781362/