我想要的是一个相当简单的二叉搜索树,通过模板标签,允许在其中使用任何数值数据,但我有一些相当讨厌的问题,我不知道如何摆脱如果有人可以提供帮助,我们将不胜感激。不断弹出的错误信息是“没有参数列表无效使用模板名称'BST'” - 坦率地说,我不知道如何解决它。它出现在bst.cpp文件中的第31,89,105,120,130,141行。鉴于我在二叉搜索树方面不够精通,我更倾向于尽可能确定答案(甚至可以确切地提及需要更改的位置和内容):
Main.cpp的
#include <iostream>
#include "bst.h"
using namespace std;
int main()
{
BST <int> tree;
tree.insert(8);
tree.insert(25);
tree.insert(99);
tree.insert(20);
tree.insert(25);
tree.insert(20);
tree.insert(2);
tree.insert(89);
tree.insert(15);
tree.insert(10);
tree.insert(30);
tree.insert(50);
tree.displayorder();
int number;
int Inputnumber;
while (true){
cout << "Choose what you want to do: " << endl << "1# Insert" << endl << "2# Display Orders" << endl << "3# Search" << endl << "4# Delete" << endl << endl << endl;
cin >> Inputnumber;
if (Inputnumber==1){
cout << endl << "Enter the number you want inserted: ";
cin >> number;
tree.insert(number);
cout << endl << endl << endl;
}
if (Inputnumber==2){
cout<<"Display Orders: " << endl;
tree.displayorder();
cout << endl << endl << endl;
}
if (Inputnumber==3){
cout<<"Enter the number you want to search for: ";
cin >> number;
tree.search(number);
cout << endl << endl << endl;
}
if (Inputnumber==4){
cout << "Enter the number you want to remove: ";
cin >> number;
tree.remove(number);
cout << endl << endl << endl;
}
}
}
BST.cpp
#include <iostream>
#include "bst.h"
using namespace std;
template <class T>
void BST<T>::preorder(node* tree)
{
if(tree == NULL){
return;
}
cout << tree->data << " ";
inorder(tree->left);
inorder(tree->right);
}
template <class T>
void BST<T>::postorder(node* tree)
{
if(tree == NULL){
return;
}
inorder(tree->left);
inorder(tree->right);
cout << tree->data << " ";
}
template <typename T>
BST::node* BST<T>::find(node* tree, T x) //ERROR HERE
{
if(tree == NULL)
return NULL;
else if(x < tree->data)
return find(tree->left, x);
else if(x > tree->data)
return find(tree->right, x);
else
return tree;
}
template <typename T>
BST<T>::BST()
{
root = NULL;
}
template <typename T>
BST<T>::~BST()
{
root = makeEmpty(root);
}
template <class T>
void BST<T>::insert(T x)
{
root = insert(x, root);
}
template <class T>
void BST<T>::remove(T x)
{
root = remove(x, root);
}
template <class T>
void BST<T>::displayorder()
{
inorder(root);
cout << endl;
preorder(root);
cout << endl;
postorder(root);
cout << endl << endl;
}
template <class T>
void BST<T>::search(T x)
{
if(root = find(root, x)){
cout << endl << "Found!" << endl;
}
else{
cout << endl << "Not Found!" << endl;
}
}
template <class T>
BST::node* BST<T>::makeEmpty(node* tree) //ERROR HERE
{
if(tree == NULL)
return NULL;
{
makeEmpty(tree->left);
makeEmpty(tree->right);
delete tree;
}
return NULL;
}
template <class T>
BST::node* BST<T>::insert(T x, node* tree) //ERROR HERE
{
if(tree == NULL)
{
tree = new node;
tree->data = x;
tree->left = tree->right = NULL;
}
else if(x < tree->data)
tree->left = insert(x, tree->left);
else if(x >= tree->data)
tree->right = insert(x, tree->right);
return tree;
}
BST::node* BST::findMin(node* tree) //ERROR HERE
{
if(tree == NULL)
return NULL;
else if(tree->left == NULL)
return tree;
else
return findMin(tree->left);
}
BST::node* BST::findMax(node* tree) //ERROR HERE
{
if(tree == NULL)
return NULL;
else if(tree->right == NULL)
return tree;
else
return findMax(tree->right);
}
template <typename T>
BST::node* BST<T>::remove(T x, node* tree) //ERROR HERE
{
node* temp;
if(tree == NULL)
return NULL;
else if(x < tree->data)
tree->left = remove(x, tree->left);
else if(x > tree->data)
tree->right = remove(x, tree->right);
else if(tree->left && tree->right)
{
temp = findMin(tree->right);
tree->data = temp->data;
tree->right = remove(tree->data, tree->right);
}
else
{
temp = tree;
if(tree->left == NULL)
tree = tree->right;
else if(tree->right == NULL)
tree = tree->left;
delete temp;
}
return tree;
}
template <class T>
void BST<T>::inorder(node* tree)
{
if(tree == NULL){
return;
}
inorder(tree->left);
cout << tree->data << " ";
inorder(tree->right);
}
BST.h
#ifndef BST_H
#define BST_H
template <class T>
class BST
{
struct node
{
T data;
node* left;
node* right;
};
node* root;
node* makeEmpty(node* tree);
node* insert(T x, node* tree);
node* findMin(node* tree);
node* findMax(node* tree);
node* remove(T x, node* tree);
void inorder(node* tree);
void preorder(node* tree);
void postorder(node* tree);
public:
BST();
~BST();
node* find(node* tree, T x);
void insert(T x);
void remove(T x);
void displayorder();
void search(T x);
};
#endif // BST_H
答案 0 :(得分:3)
例如,在
中BST::node* BST<T>::find(node* tree, T x)
您忘记了第一个<T>
的{{1}}组件。
应该是
BST
所有其他错误属于同一类型。
答案 1 :(得分:3)
您明显使用的BST
等类模板不是类型。它是创建类类型的秘诀。错误消息试图告诉您的是(几乎)您使用名称BST
的任何地方,您需要在<
尖括号>
内紧接着提供模板参数。
例如,在
中template <class T>
BST::node* BST<T>::makeEmpty(node* tree) //ERROR HERE
编译器抱怨返回类型中的第一个BST
实例,而不是正确指定BST<T>
作为类类型的实例。这应该是:
template <class T>
BST<T>::node* BST<T>::makeEmpty(node* tree)
[这个一般规则至少有两个例外。一个是单独的模板名称可以用作另一个模板的模板参数,该模板需要模板而不是类型或值。
另一个被称为&#34;注入类名&#34;:在类模板的范围内,包括类模板成员,您可以只使用模板的名称作为&#34的别名;电流&#34;专业化。
所以实际上你也可以使用尾随返回类型并执行:
template <class T>
auto BST<T>::makeEmpty(node* tree) -> BST::node*
在上面,由于返回类型现在位于BST<T>::
之后而不是之前,它现在位于类模板的范围内,因此您只能使用BST
作为别名对于BST<T>
。]