【发布时间】:2012-06-13 09:53:31
【问题描述】:
我会尽量保持代码简短。
我正在尝试使用模板制作一个 B inary S 搜索 T ree(简称 BST)。
在我的添加函数中,我遇到了一个错误,我确定我以某种方式滥用了模板
由于模板,所有这些代码都在一个 .h(头文件)文件中。
编辑: const Type & 错误是因为我摆弄,它实际上不在我编译的代码中,而是来自上一个关于堆栈溢出的问题
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); // key, data
bool add (int, Type);
};
添加函数:
template <typename Type>
bool BSTNode<Type>::add(int newKey, Type newData) {
if (newKey < this->key) {
if (left == NULL) {
this->left = new BSTNode<Type>(int newKey, Type newData);
}
} else {
this->right = new BSTNode<Type>(int newKey, Type newData);
}
return false;
}
这是我得到错误的地方:
this->left = new BSTNode<Type>(int newKey, Type newData);
int 之前的预期主表达式
【问题讨论】:
-
我们已经知道 BST 代表什么。 ;)
标签: c++ templates pointers binary-search-tree dereference