errorC2512和错误C2955使用c++模板

errorC2512 and error C2955 using C++ template

本文关键字:c++ 模板 使用 C2955 错误 errorC2512      更新时间:2023-10-16
#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:使用模板需要模板参数列表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
}

您需要将模板参数添加到结构中的成员中,例如

template<class T>
struct avl_node
{
    T data;
    struct avl_node *left;
    struct avl_node *right;
};
应该

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

对于您稍后定义的创建或声明avl_node*变量的函数,这些函数应该使用avl_node<T>*变量。