Oct 5, 2011 at 12:03am UTC
I have a homework in which I need to look for a node in 1 of the many trees I had to program, using pre-order using a function template. My code is:
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
class Searcher{
public :
Searcher(){}
~Searcher(){}
template < class Node, class Tree >
Node* search(char label, Tree tree){
Node * node = tree.getRoot();
node = search1(label,node,tree);
return node
}
private :
template < class Node, class Tree >
Node* search1(char label, Node* node, Tree tree){
Node * result = 0;
if (tree.label(node) == label){
result = node;
}else {
result = search1(label, tree.leftChild(node), tree);
if (result== 0){
result = search1(label, tree.rightBrother(node), tree);
}
}
return result;
}
};
And in my main I write:
1 2 3 4 5 6 7 8 9 10 11 12 13
#include .....
int main(int argc, char *argv[]){
Searcher searcher;
Tree4 tree; //A n-ary tree
tree.setRoot('a' );
Tree4::Node * root = tree.getRoot();
tree.addChild('b' ,0,root);
Tree4::Node * node = root.leftChild(root);
tree.addChild('c' ,0,node);
tree.addChild('d' ,node,root);
searcher.search('a' ,tree);
return 0;
}
But it gives me: 19 no matching function for call to `Searcher::search(char, Tree4&)' error Plz help
Last edited on Oct 5, 2011 at 12:09am UTC
Oct 5, 2011 at 1:37am UTC
The template function Searcher::search requires the Node class and the Tree class. Without testing, I think it is fixed by means of explicitly giving the data types:
searcher.search<Tree4::Node, Tree4>('a' , tree);
Try it out. I don't really want to try myself.