Saturday 15 February 2014

c++ - Template with struct -



c++ - Template with struct -

i designing binary search tree allows user come in value of data-type, not int.to accomplish this,i trying utilize template struct. defined struct follows

template <class t> struct node { struct node *left; t info; struct node *right; }*root;

now trying utilize in class called bst(binary search tree)

template <class t> class bst { public: void insert(node *,node *); void inorder(node *); };

but compiler throwing error, template declaration of 'node< t >* root'. how can utilize template struct variables ?

you can't declare root after template class declaration, because template argument can't deduced, could:

template <class t> struct node { struct node *left; t info; struct node *right; }; node <int> * root;

and should appoint template parameter type when utilize node, such as:

template <class t> class bst { public: void insert(node<t>*, node<t>*); void inorder(node<t>*); };

c++ class templates struct binary-search-tree

No comments:

Post a Comment