c++ - 使用 C++ 模板时出现错误 C2512 和错误 C2955

标签 c++ templates avl-tree

#include<iostream>
#include<cstdio>
#include<sstream>
#include<algorithm>
#define pow2(n) (1 << (n))
using namespace std;


template<class T> struct avl_node
{
    T data;
    struct avl_node *left;
    struct avl_node *right;
};
avl_node<int>* root;

template<class T>
class avlTree
{


    public:
        int height(avl_node<T> *);
        int diff(avl_node<T> *);
        avl_node<T> *rr_rotation(avl_node<T> *);
        avl_node<T> *ll_rotation(avl_node<T> *);
        avl_node<T> *lr_rotation(avl_node<T> *);
        avl_node<T> *rl_rotation(avl_node<T> *);
        avl_node<T> *balance(avl_node<T> *);
        avl_node<T> *insert(avl_node<T> *, int );
        void display(avl_node<T> *, int);
                avlTree()
        {
            root = NULL;
        }
};


int main()
{
    int choice, item;
    avlTree<int> avl;
    while (1)
    {
        cout<<"\n---------------------"<<endl;
        cout<<"AVL Tree "<<endl;
        cout<<"\n---------------------"<<endl;
        cout<<"1.Insert Element into the tree"<<endl;
        cout<<"2.Display Balanced AVL Tree"<<endl;
        cout<<"3.Exit"<<endl;
        cout<<"Enter your Choice: ";
        cin>>choice;
        switch(choice)
        {
        case 1:
            cout<<"Enter value to be inserted: ";
            cin>>item;
            root = avl.insert(root, item);
            break;
        case 2:
            if (root == NULL)
            {
                cout<<"Tree is Empty"<<endl;
                continue;
            }
            cout<<"Balanced AVL Tree:"<<endl;
            avl.display(root, 1);
            break;
        case 3:
            exit(1);    
            break;
        default:
            cout<<"Wrong Choice"<<endl;
        }
    }
    return 0;
}

template<class T>
int avlTree<T>::height(avl_node<T> *temp)
{
    int h = 0;
    if (temp != NULL)
    {
        int l_height = height (temp->left);
        int r_height = height (temp->right);
        int max_height = max (l_height, r_height);
        h = max_height + 1;
    }
    return h;
}

template<class T>
int avlTree<T>::diff(avl_node<T> *temp)
{
    int l_height = height (temp->left);
    int r_height = height (temp->right);
    int b_factor= l_height - r_height;
    return b_factor;
}

template<class T>
avl_node<T> *avlTree<T>::rr_rotation(avl_node<T> *parent)
{
    avl_node *temp;
    temp = parent->right;
    parent->right = temp->left;
    temp->left = parent;
    return temp;
}

template<class T>
avl_node<T> *avlTree<T>::ll_rotation(avl_node<T> *parent)
{
    avl_node *temp;
    temp = parent->left;
    parent->left = temp->right;
    temp->right = parent;
    return temp;
}

template<class T>
avl_node<T> *avlTree<T>::lr_rotation(avl_node<T> *parent)
{
    avl_node *temp;
    temp = parent->left;
    parent->left = rr_rotation (temp);
    return ll_rotation (parent);
}

template<class T>
avl_node<T> *avlTree<T>::rl_rotation(avl_node<T> *parent)
{
    avl_node *temp;
    temp = parent->right;
    parent->right = ll_rotation (temp);
    return rr_rotation (parent);
}

template<class T>
avl_node<T> *avlTree<T>::balance(avl_node<T> *temp)
{
    int bal_factor = diff (temp);
    if (bal_factor > 1)
    {
        if (diff (temp->left) > 0)
            temp = ll_rotation (temp);
        else
            temp = lr_rotation (temp);
    }
    else if (bal_factor < -1)
    {
        if (diff (temp->right) > 0)
            temp = rl_rotation (temp);
        else
            temp = rr_rotation (temp);
    }
    return temp;
}

template<class T>
avl_node<T> *avlTree<T>::insert(avl_node<T> *root, int value)
{
    if (root == NULL)
    {
        root = new avl_node;
        root->data = value;
        root->left = NULL;
        root->right = NULL;
        return root;
    }
    else if (value < root->data)
    {
        root->left = insert(root->left, value);
        root = balance (root);
    }
    else if (value >= root->data)
    {
        root->right = insert(root->right, value);
        root = balance (root);
    }
    return root;
}

template<class T>
void avlTree<T>::display(avl_node<T> *ptr, int level)
{
    int i;
    if (ptr!=NULL)
    {
        display(ptr->right, level + 1);
        printf("\n");
        if (ptr == root)
        cout<<"Root -> ";
        for (i = 0; i < level && ptr != root; i++)
            cout<<"        ";
        cout<<ptr->data;
        display(ptr->left, level + 1);
    }
}

你能帮我一下吗?我今天必须完成这个任务。

1 错误 C2955:avl_node:使用模板需要模板参数列表 2 错误 C2512:avl_node:无法将函数定义与现有声明匹配

最佳答案

除了 Cyber​​ 的回答之外,您还需要对 temp 变量进行更改。

template<class T>
avl_node<T> *avlTree<T>::rr_rotation(avl_node<T> *parent)
{
    avl_node<T> *temp;
    //rest of code
}

template<class T>
avl_node<T> *avlTree<T>::ll_rotation(avl_node<T> *parent)
{
    avl_node<T> *temp;
    // rest of code
}

template<class T>
avl_node<T> *avlTree<T>::lr_rotation(avl_node<T> *parent)
{
    avl_node<T> *temp;
    //rest of code
}

template<class T>
avl_node<T> *avlTree<T>::rl_rotation(avl_node<T> *parent)
{
    avl_node<T> *temp;
    //rest of code
}

template<class T>
avl_node<T> *avlTree<T>::insert(avl_node<T> *root, int value)
{
    if (root == NULL)
    {
        root = new avl_node;        //make it avl_node<T>

    //rest of code
}

关于c++ - 使用 C++ 模板时出现错误 C2512 和错误 C2955,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30309749/

相关文章:

templates - 有没有办法使用 mixin 或模板从 D 中的类生成接口(interface)?

c - 完美平衡二叉搜索树

c++ - 如何将非静态成员函数作为回调传递?

c++ - unique_ptr 参数类型的模板参数推导指南?

c++ - 函数是否在退出作用域时清除动态内存?

c++ - 如果在模板特化中使用 "typename"没有区别

c++ - AVL Delete 方法行为奇怪,C++

c++ - 实现 AVL 树

具有动态分配数组的类的 C++ 复制构造函数

c++ - 是否可以有效地检查 STL std::deque 删除是否成功?