Reputation: 2881
I'll try and keep the code short.
I'm trying to make a B inary S earch T ree (BST for short) using templates.
In my add function I'm getting an error and I'm sure I'm misusing templates somehow
All this code is in a .h (header) file because of templates.
EDIT: The const Type & mistake was because of me fiddling around, it was not actually in the code I compile, but from a previous question on Stack overflow
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);
};
The add function:
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 is where I get the error:
this->left = new BSTNode<Type>(int newKey, Type newData);
Expected primary expression before int
Upvotes: 0
Views: 1366
Reputation: 258618
The error is pretty clear:
bool add (int, Type);
vs
bool add(int newKey, const Type &newData)
You should change your declaration in the class definition to:
bool add (int, const Type&);
and remove the types from the statements:
this->right = new BSTNode<Type>(int newKey, Type newData);
this->right = new BSTNode<Type>(int newKey, Type newData);
should be
this->right = new BSTNode<Type>(newKey, newData);
this->right = new BSTNode<Type>(newKey, newData);
Upvotes: 1
Reputation: 6155
You are not misuing templates specifically, but you are misusing parameters!
this->left = new BSTNode<Type>(int newKey, Type newData);
should look more like
this->left = new BSTNode<Type>(newKey, newData);
Upvotes: 2
Reputation: 11232
It should be this->left = new BSTNode<Type>(newKey, newData);
without any types.
Upvotes: 1