【发布时间】:2014-06-24 03:51:58
【问题描述】:
我正在尝试将“普通”类重写为模板类。我遇到了一个问题——函数int main()。我不确定问题是否确实在这里,但编译器报告“在'b'之前缺少模板参数”。我在互联网上找不到与类似问题相关的示例代码。你能帮帮我吗?
这是我找到的代码:
#include <process.h>
#include <conio.h>
#include <iostream>
#include <cstdlib>
using namespace std;
class BinarySearchTree
{
private:
struct tree_node
{
tree_node* left;
tree_node* right;
int data;
};
tree_node* root;
public:
BinarySearchTree()
{
root = NULL;
}
bool isEmpty() const { return root==NULL; }
void insert(int);
bool search(int);
};
//----------------------------------------------------
bool BinarySearchTree::search(int d)
{
tree_node* temp = root;
while (temp != NULL)
{
if (temp->data == d)
{
cout<<"Tree contains this node"<<endl;
return true;
}
else
{
if (d > temp->data)
{
temp = temp->right;
}
else
{
temp = temp->left;
}
}
}
cout<<"Tree does not contain this node"<<endl;
return false;
}
void BinarySearchTree::insert(int d)
{
tree_node* t = new tree_node;
tree_node* parent;
t->data = d;
t->left = NULL;
t->right = NULL;
parent = NULL;
if(isEmpty()) root = t;
else
{
tree_node* curr;
curr = root;
// Find the Node's parent
while(curr)
{
parent = curr;
if(t->data > curr->data) curr = curr->right;
else curr = curr->left;
}
if(t->data < parent->data)
parent->left = t;
else
parent->right = t;
}
}
//----------------------------------------------------
int main()
{
BinarySearchTree b;
int ch,tmp,tmp1,tmp2;
while(1)
{
cout<<endl<<endl;
cout<<" Binary Search Tree Operations "<<endl;
cout<<" ----------------------------- "<<endl;
cout<<" 1. Insertion/Creation "<<endl;
cout<<" 2. Does Binary Searching Tree contains this node? "<<endl;
cout<<" 3. Exit "<<endl;
cout<<" Enter your choice : ";
cin>>ch;
switch(ch)
{
case 1 : cout<<" Enter Number to be inserted : ";
cin>>tmp;
b.insert(tmp);
break;
case 2 : cout<<" Enter data to be found : ";
cin>>tmp;
b.search(tmp);
break;
case 3 : system("pause");
return 0;
break;
}
}
}
我试图重写它:
#include <process.h>
#include <conio.h>
#include <iostream>
#include <cstdlib>
using namespace std;
template <class T>
class BinarySearchTree
{
private:
struct tree_node
{
tree_node* left;
tree_node* right;
T data;
};
tree_node* root;
public:
BinarySearchTree<T>()
{
root = NULL;
}
T isEmpty() const { return root==NULL; }
T insert(T);
T search(T);
};
//----------------------------------------------------
template <class T>T BinarySearchTree<T>::search(T d)
{
tree_node* temp = root;
while (temp != NULL)
{
if (temp->data == d)
{
cout<<"Tree contains this node"<<endl;
return true;
}
else
{
if (d > temp->data)
{
temp = temp->right;
}
else
{
temp = temp->left;
}
}
}
cout<<"Tree does not contain this node"<<endl;
return false;
}
template <class T>T BinarySearchTree<T>::insert(T d)
{
tree_node* t = new tree_node;
tree_node* parent;
t->data = d;
t->left = NULL;
t->right = NULL;
parent = NULL;
if(isEmpty()) root = t;
else
{
tree_node* curr;
curr = root;
while(curr)
{
parent = curr;
if(t->data > curr->data) curr = curr->right;
else curr = curr->left;
}
if(t->data < parent->data)
parent->left = t;
else
parent->right = t;
}
}
//----------------------------------------------------
int main()
{
BinarySearchTree b;
int ch,tmp,tmp1,tmp2;
while(1)
{
cout<<endl<<endl;
cout<<" Binary Search Tree Operations "<<endl;
cout<<" ----------------------------- "<<endl;
cout<<" 1. Insertion/Creation "<<endl;
cout<<" 2. Does Binary Searching Tree contains this node?"<<endl;
cout<<" 3. Exit "<<endl;
cout<<" Enter your choice : ";
cin>>ch;
switch(ch)
{
case 1 : cout<<" Enter Number to be inserted : ";
cin>>tmp;
b.insert(tmp);
break;
case 2 : cout<<" Enter data to be found : ";
cin>>tmp;
b.search(tmp);
break;
case 3 : system("pause");
return 0;
break;
}
}
}
【问题讨论】:
-
BinarySearchTree一个模板,而不是一个类型。它需要模板参数......就像你不做std::vector v;一样。 -
你的重写没有任何意义。为什么
isEmpty()、insert()和search()返回T?而这个BinarySearchTree<T>() { ... }应该是BinarySearchTree() { ... } -
@Praetorian 我正在关注指南codingunit.com/cplusplus-tutorial-templates
-
@Anakin,
Stack<int> int_stack; -
“这是我找到的代码” - 这是哪个?你找到的代码还是重写后的代码?无论如何,您可能会更好地学习语言。正如许多人所证明的那样,没有捷径可走。
标签: c++ algorithm binary-search-tree class-template