试图做一个B inary S earch Tree(简称BST)使用模板。
当我尝试创建 BST 的新实例时,出现意外错误。我希望解决方案不涉及指针,因为我想将它们保持在最低限度。
现在我有:
template <typename Type>
class BST { // The binary search tree containing nodes
private:
BSTNode<Type> *root; // Has reference to root node
public:
BST ();
bool add (int, Type);
};
以及节点类型:
EDIT:当我删除代码以取消阻碍文本时,我忘记了构造函数,现在它已被添加
template <typename Type>
class BSTNode { // Binary Search Tree nodes
private:
int key; // we search by key, no matter what type of data we have
Type data;
BSTNode *left;
BSTNode *right;
public:
BSTNode (int, Type&);
bool add (int, Type);
};
EDIT2:这是实际的构造函数
template <typename Type>
BSTNode<Type>::BSTNode (int initKey, Type &initData) {
this->key = initKey;
this->data = initData;
this->left = NULL;
this->right = NULL;
}
我想尝试测试是否有任何功能有效/无效
BSTNode<int> data = new BSTNode (key, 10);
我得到: BSTNode 之前的预期类型说明符。我不知道我做错了什么,但我确实希望的一件事是我不必使用数据作为指针。
BSTNode<int> data = new BSTNode<int> (key, 10);
也不起作用,看来它相信< int >
is < & int>
它不匹配