problem with templates tree
Write your question here.
Hello
I wrote this code, the function need to return pointer to node of a binary tree:
1 2 3 4 5 6 7 8 9 10 11
|
template <class T>
Tree<T>::Node<T>* SearchTree<T>::searchHelp(T val)
{
if(Tree<T>::root->value ==val)
return Tree<T>::root;
else
{
return (Tree<T>::root->right).searchHelp();
return (Tree<T>::root->left).searchHelp();
}
}
|
and I have 3 problems:
1:error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
2:error C2936: 'Tree<T>::Node<T>' : template-class-id redefined as a global data variable
3:error C2143: syntax error : missing ';' before '*'
Hope to have an answer fast
Thank You
please please please help us!!!!
and if you dont kkow just write us that.....
Are you sure all those errors are in that code, not in another part of the program?
yes!
those errors are at line 2
show us the decleration of Tree and Node as well as one of the usages of them please.
Also note that only the first return statement will be invoked!
you will only return 1, not 2 Tree<T>::Node<T>
1 2
|
return (Tree<T>::root->right).searchHelp();
return (Tree<T>::root->left).searchHelp();
|
why do you access the elements like this?
Tree<T>::root
is root a static member?
shouldn't it look like this?
this->root
Last edited on
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
//-----------------------------------
// class Tree (Binary Trees)
// process nodes in Pre/In/Post order
//-----------------------------------
template <class T> class Tree
{
protected:
//--------------------------------------------------------
// inner class Node
// a single Node from a binary tree
//--------------------------------------------------------
template <class T> class Node
{
public:
Node * left;
Node * right;
T value;
Node(T val)
: value(val), left(NULL), right(NULL){}
Node(T val, Node<T> * l, Node<T> * r)
: value(val), left(l), right(r){}
}; //end of Node class
Node<T>* root;
public:
Tree() {root=NULL;} // initialize tree
~Tree();
int isEmpty() const;
void clear() { clear(root); root=NULL;}
void preOrder() { preOrder(root); }
void inOrder() { inOrder(root); }
void postOrder() { postOrder(root); }
int leaves();
int height();
void reflect();
int onlyLeftSon ();
virtual void process(T val) {cout<<val<<" ";}
virtual void add (T val)=0;
virtual bool search (T val) =0;
virtual void remove (T val) =0;
private:
void clear(Node<T> * current);
void preOrder(Node<T> * current);
void inOrder(Node<T> * current);
void postOrder(Node<T> * current);
};
#endif
|
I wrote 2 times return because it's a recursive function
I wrote 2 times return because it's a recursive function |
The function ends at the first return statement
You have pure virtual functions here so you should not be able to instanciate a Tree at all...
1 2 3
|
virtual void add (T val)=0;
virtual bool search (T val) =0;
virtual void remove (T val) =0;
|
okay.
Thank You!!
Topic archived. No new replies allowed.