我正在尝试实现一个AVL树,似乎遇到了如何使用我的Node类的问题。我收到错误C4430:第二个getHeight缺少类型说明符我以为我将类型指定为子树的节点?
template <typename T>
class SetAVL
{
public:
int getHeight()
{
return getHeight(root);
}
// Complaining about this line
int getHeight(const Node<T> *subtree)
{
// If we are at a leaf
if (subtree == NULL)
return 0;
return 1 + max(getHeight(subtree->left), getHeight(subtree->right));
}
void add(Node<T> *item)
{
Node<T> *t = new Node<T>(item);
insert(root, t);
}
void insert(Node<T> *root, Node<T> *t)
{
if (root == NULL)
root = t;
else
{
if (t->item < root->item)
insert(root->left, t);
else if (t->item != root->item)
insert(root->right, t);
else
delete t;
}
}
protected:
template <typename T>
class Node
{
public:
T item;
Node *left;
Node *right;
int height;
Node(T item)
{
this->item = item;
this->left = NULL;
this->right = NULL;
this->height = 0;
}
}
Node<T> *root;
int treeSize;
}
答案 0 :(得分:2)
似乎需要为左右点指定正确的类型
Node *left;
Node *right;
应该是
Node<T> *left;
Node<T> *right;
我在使用之前移动了Node定义。顺便说一句,你输了';'在类节点定义的末尾。
template <typename T>
class SetAVL
{
protected:
template <typename T>
class Node
{
public:
T item;
Node<T> *left;
Node<T> *right;
int height;
Node(T item)
{
this->item = item;
this->left = NULL;
this->right = NULL;
this->height = 0;
}
};
Node<T> *root;
int treeSize;
public:
int getHeight()
{
return getHeight(root);
}
int getHeight(const Node<T> *subtree)
{
// If we are at a leaf
if (subtree == NULL)
return 0;
return 1 + max(getHeight(subtree->left), getHeight(subtree->right));
}
void add(Node<T> *item)
{
Node<T> *t = new Node<T>(item);
insert(root, t);
}
void insert(Node<T> *root, Node<T> *t)
{
if (root == NULL)
root = t;
else
{
if (t->item < root->item)
insert(root->left, t);
else if (t->item != root->item)
insert(root->right, t);
else
delete t;
}
}
};
希望它有所帮助!