我一直试图实现和AVL Tree,但我继续得到这两个错误C2954和C2955并且不知道如何解决它们

时间:2014-07-27 18:28:57

标签: 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));
}

1 个答案:

答案 0 :(得分:2)

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

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

我无法理解其他错误,但可能是由于前面的错误造成的。由于它使用的名称未在发布的类的摘录中声明,因此它也可能是不同的。