为什么编译器不将 Node 识别为一种类型?它是 AVLTree 中的私有 class
Why does the compiler not recognize Node as a type? It is a private class within AVLTree
class AVLTree{
struct Node {
K key;
V value;
Node* left;
Node* right;
int height;
/**
* Node constructor; sets children to point to `NULL`.
* @param newKey The object to use as a key
* @param newValue The templated data element that the constructed
* node will hold.
*/
Node(const K& newKey, const V& newValue)
: key(newKey), value(newValue), left(NULL), right(NULL), height(0)
{
}
};
============================================= =================
Node* AVLTree::findParent(Node *¤t, Node *& child ) {
if (current == NULL) {
return NULL;
}
if (current->right == child || current->left == child) {
return current;
} else {
findParent(current->right, child);
findParent(current->left, child);
}
}
尝试编写一个函数来查找 AVL 树中节点的父节点,以便我可以在旋转函数中使用它。但是,每当我尝试编译时,我都会收到此错误:
tests/../avltree.cpp:72:1: fatal error: unknown type name 'Node'
Node* AVLTree::findParent(Node *¤t, Node *& child ) {
为什么会这样?
Find parent 在 avltree.cpp 中并且在 AVLTree class 中被列为私有成员,请问这是什么问题?
我也试过 AVLTree::Node,但后来出现了这个错误:
AVLTree::Node* AVLTree::findParent(Node *¤t, Node *& child ) {
^
tests/../avltree.h:20:7: note: 'AVLTree' declared here
class AVLTree
基本问题是 return 类型是在全局范围内解析的,而不是在方法范围内(因为它是 before 方法名称及其范围说明符)。所以你需要明确地限定它的范围:
AVLTree::Node* AVLTree::findParent(Node *¤t, Node *& child ) {
class AVLTree{
struct Node {
K key;
V value;
Node* left;
Node* right;
int height;
/**
* Node constructor; sets children to point to `NULL`.
* @param newKey The object to use as a key
* @param newValue The templated data element that the constructed
* node will hold.
*/
Node(const K& newKey, const V& newValue)
: key(newKey), value(newValue), left(NULL), right(NULL), height(0)
{
}
};
============================================= =================
Node* AVLTree::findParent(Node *¤t, Node *& child ) {
if (current == NULL) {
return NULL;
}
if (current->right == child || current->left == child) {
return current;
} else {
findParent(current->right, child);
findParent(current->left, child);
}
}
尝试编写一个函数来查找 AVL 树中节点的父节点,以便我可以在旋转函数中使用它。但是,每当我尝试编译时,我都会收到此错误:
tests/../avltree.cpp:72:1: fatal error: unknown type name 'Node'
Node* AVLTree::findParent(Node *¤t, Node *& child ) {
为什么会这样? Find parent 在 avltree.cpp 中并且在 AVLTree class 中被列为私有成员,请问这是什么问题?
我也试过 AVLTree::Node,但后来出现了这个错误:
AVLTree::Node* AVLTree::findParent(Node *¤t, Node *& child ) {
^
tests/../avltree.h:20:7: note: 'AVLTree' declared here
class AVLTree
基本问题是 return 类型是在全局范围内解析的,而不是在方法范围内(因为它是 before 方法名称及其范围说明符)。所以你需要明确地限定它的范围:
AVLTree::Node* AVLTree::findParent(Node *¤t, Node *& child ) {