How can I call a function with a template recursively?

I am trying to create a tree class in C ++ and I'm not sure how to use templates when I use recursion.

For example, I have the following function:

template <typename Data> void destroyTree(typename AVLTree<Data>::Node* element) { if(!element) { return; } destroyTree(element->getLeft()); destroyTree(element->getRight()); delete element; } 

or should be:

 template <typename Data> void destroyTree(typename AVLTree<Data>::Node* element) { if(!element) { return; } destroyTree<Data>(element->getLeft()); destroyTree<Data>(element->getRight()); delete element; } 

then if I call it suppose from the following function:

 template <typename Data> void AVLTree<Data>::function() { destroyTree(root); } 

or

 template <typename Data> void AVLTree<Data>::function() { destroyTree<Data>(root); } 

I tried to combine most of the above features, but always get an error message. this is usually a non matching function for call to

If anyone has experience with this issue, please help me.

Thanks.

If I use the Nawaz suggestion, I get the following error:

The internal Builder is used to build ** g ++ -O0 -g3 -Wall -c -fmessage-length = 0 -oAVLTest.o .. \ AVLTest.cpp g ++ -oAVLTree.exe AVLTest.o AVLTest.o: In function ZN15Data_Structures7AVLTreeIiE4Node7getLeftEvEvEvEtVe : / Users / Alex / workspace /AVLTree/Debug/../AVLTree.h :( text $ _ZN15Data_Structures11destroyTreeIiEEvPNS_7AVLTreeIT_E4NodeE [void. Data_Structures :: destroyTree (Data_Structures :: AVLTree :: Node *)] + 0_Data_Data_) + 0: 0_Data_Default_Default_Default_Default : AVLTree :: Node :: ~ Node () 'collect2: ld returned 1 exit status Build error, build stopped Time spent on consumption: 532 ms.

what can i do with this?

all relevant codes are here .

+4
source share
1 answer

It is right:

 //in the first case destroyTree<Data>(element->getLeft()); destroyTree<Data>(element->getRight()); //in the second case destroyTree<Data>(root); 

That is, you must specify the template argument, because it cannot be deduced by the compiler from the function argument, because it is not an inferred context.

+5
source

All Articles