I am trying to make a BST data structure, in it a method searchUtil(T x)
finds the node having data x, and returns its address, and the address of its parent in a 'pair' object. Without templating, the method works fine. This is the code:
#include<iostream>
using namespace std;
template<typename T>
class BST{
public:
struct node{
T data;
node *left, *right;
node(T _data):data(_data){
left = right = NULL;
}
node():data(){
left = right = NULL;
}
};
node* root;
long long int _size;
BST(){root=NULL;_size=0;}
std::pair<node*, node*> searchUtil(T);
};
template<typename T>
typename std::pair<BST<T>::node*, BST<T>::node*> BST<T>::searchUtil(T x){
node *cur = root, *trail = NULL;
while(cur != NULL){
if(cur->data < x){
trail = cur;
cur = cur->right;
}else if(cur->data > x){
trail = cur;
cur = cur->left;
}else{
break;
}
}
return pair<node*,node*> (cur, trail);
}
I have tried to change the code to remove the errors, but to no merit.
The errors are:
||=== Build: Debug in testBench (compiler: GNU GCC Compiler) ===|
error: type/value mismatch at argument 1 in template parameter list for 'template<class _T1, class _T2> struct std::pair'|
error: expected a type, got '(BST<T>::node * <expression error>)'|
error: template argument 2 is invalid|
error: 'BST' in namespace 'std' does not name a template type|
error: expected unqualified-id before '<' token|
||=== Build failed: 5 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
And I cannot make anything of them. Any insight to why this is happening would be greatly helpful.
I am also confused by how if I don't write BST<T>::node*
and simply write node*
, I would get an error of node not being defined