c++ - 我一直在尝试实现和 AVL 树,但我不断收到这 2 个错误 C2954 和 C2955,并且不知道如何解决它们

标签 c++ templates data-structures tree avl-tree

这是我的代码 我已经在相应的行旁边写下了发生的错误消息

//AVL Tree implemantation
template<class Element>
class AVLtree
{
    public:
      int height(AVLnode<Element>*)const;
    int max(int,int)const;
};
//Function to get the max
template<class Element>
int AVLtree<Element>::max(int a, int b)
{
return ((a>b)?a:b);
} //Error:'AVLtree<Element>::max' : unable to resolve function overload
//Function to calculate the height
template<class Element> //Error:error C2954: template definitions cannot nest
int AVLtree<Element>::balanceFactor(AVLnode<Element>* p)
{
return (height(p->left) - height(p->right));
}

最佳答案

第一个错误是您将 max() 声明为 const 成员函数,但您试图将其定义为非 const成员函数。您需要在定义中添加 const:

template<class Element>
int AVLtree<Element>::max(int a, int b) const {
    return std::max(a, b);
}

我不太明白另一个错误,但它可能是由前面的错误引起的。由于它使用的名称未在发布的类的摘录中声明,因此它也可能有所不同。

关于c++ - 我一直在尝试实现和 AVL 树,但我不断收到这 2 个错误 C2954 和 C2955,并且不知道如何解决它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24984199/

相关文章:

c++ - 是否有泛化 int4、uint4、float4 等的模板化类型?

templates - 根据模板参数选择参数类型

perl - 如何在Perl中创建多维数组?

java - java中如何从List中删除当前元素

c++ - 为什么不为左值和右值重载 operator[]?

c# - 媒体基础 : ReadSample - Access Violation Exception

c++ - 在 C++17 中弃用 `std::result_of` 的原因是什么?

algorithm - 如何将元素插入已排序的堆中以使其保持排序?

c++ - 无序多重映射查找所有值

c++ - int64_t 指针转换为 AVX2 intrinsic _m256i