我有这个每当我想在C ++上使用模板在二叉树上添加元素并且我在修复它时遇到困难,
所以这里的代码是Tree.c文件:
template <class Whatever>
ostream & operator << (ostream &, const TNode<Whatever> &);
template <class Whatever>
struct TNode {
long balance;
Whatever data;
long height;
TNode<Whatever>* left;
long & occupancy;
TNode<Whatever>* right;
unsigned long & tree_count;
TNode (const Whatever & element, Tree<Whatever> & theTree)
:balance (0), data (element), height(0), left(0),
occupancy(theTree.occupancy), right(0),
tree_count (theTree.tree_count) {
occupancy++;
}
TNode (const Whatever & element, TNode<Whatever> & parentTNode)
: balance(0), data (element), height (0), left (0),
occupancy(parentTNode.occupancy), right (0),
tree_count (parentTNode.tree_count) {
occupancy++;
}
template <class Whatever>
unsigned long Tree<Whatever> :: Insert (const Whatever & element) {
//check to see if the tree is empty
if(root == NULL)
{
root = new TNode<Whatever>(element); //this is keep on giving me the no
//matching function error
return TRUE;
}
return FALSE;
}
这是我的Tree.h文件:
template <class Whatever>
struct TNode;
template <class Whatever>
class Tree {
friend struct TNode<Whatever>;
long occupancy;
TNode<Whatever> * root;
unsigned long tree_count;
static int debug;
public:
//here are my constructor and destructor
unsigned long Insert (const Whatever &);
这是我的Driver.h文件:
class Student {
friend ostream & operator << (ostream &, const Student &);
char name[20];
long studentnum;
public:
//here are my constructor,copy constructor, and deconstructor
每当我尝试编译时,我都会收到错误消息,
Tree.c:在成员函数'long unsigned int Tree :: Insert(const Whatever&amp;)[with Whatever = Student]:
Tree.c:140:错误:没有匹配函数来调用'TNode :: TNode(const Student&amp;)'
Tree.c:48:注意:候选人是:TNode :: TNode(const Whatever&amp;,TNode&amp;)[与Whatever = Student]
Tree.c:40:注意:TNode :: TNode(const Whatever&amp;,Tree&amp;)[with Whatever = Student]
Tree.c:31:注意:TNode :: TNode(const TNode&amp;)
任何人都知道如何修复此错误??
答案 0 :(得分:1)
你的两个TNode
构造函数都有两个参数。你只传递了一个。