gpt4 book ai didi

c++ - 使用 C++ 模板的错误 C2512 和错误 C2955

转载 作者:搜寻专家 更新时间:2023-10-31 01:00:46 26 4
gpt4 key购买 nike

#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 error C2955: avl_node: 使用模板需要模板参数列表2 error 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com