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 類中被列為私有成員,那么有什么問題呢?
我也試過做 AVLTree::Node,但后來得到了這個錯誤:
AVLTree::Node* AVLTree::findParent(Node *¤t, Node *& child ) {
^
tests/../avltree.h:20:7: note: 'AVLTree' declared here
class AVLTree
uj5u.com熱心網友回復:
基本問題是回傳型別是在全域范圍內決議的,而不是在方法范圍內(因為它在方法名稱及其范圍說明符之前)。所以你需要明確地確定它的范圍:
AVLTree::Node* AVLTree::findParent(Node *¤t, Node *& child ) {
uj5u.com熱心網友回復:
我忘記了模板引數。這就是問題所在,我沒有在這里包含模板。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/321223.html
