Reputation: 1382
Reduced to basic form, I'm trying to do something like this with a binary search tree:
template <class Item, class Key>
class bst
{
public:
Item val;
Key key;
bst *left;
bst *right;
private:
};
template <class Item, class Key, class Process, class Param>
void preorder_processing1(bst<Item, Key> *root, Process f, Param g)
{
if (root == NULL) return;
f(root->val, g);
preorder_processing1(root->left, f, g);
preorder_processing1(root->right, f, g);
}
template <class Item, class Key>
void print_path(const bst<Item, Key>* root, const Key target)
{
if (root->key != target)
{
cout << root->key << " " << root->val << endl;
if (target < root->key)
print_path(root->right, target);
else
print_path(root->left, target);
}
else
cout << root->key << " " << root->val << endl;
}
template <class Item, class Key>
void print_if_leaf(const bst<Item, Key>* test_node, const bst<Item, Key>* root)
{
if (test_node->right == NULL && test_node->left == NULL)
{
print_path(root, test_node->key);
cout << endl;
}
}
template <class Item, class Key>
void print_paths_bst(const bst<Item, Key>* root)
{
preorder_processing1(root, print_if_leaf, root);
}
When I call print_paths_bst, I get this: error: no matching function for call to ‘preorder_processing1(const bst<int, int>*&, <unresolved overloaded function type>, const bst<int, int>*&)’
I've tried forcing a cast on the preorder_processing1 call like
preorder_processing1(root, print_if_leaf<Item, Key>, root);
...but this also hasn't resolved the problem.
Does anyone see what's wrong with this?
Upvotes: 0
Views: 3217
Reputation: 91270
template <class Item, class Key, class Process, class Param>
void preorder_processing1(bst<Item, Key> *root, Process f, Param g)
This needs to take a const bst<Item, Key> *
Additionally, your call to f(root->val, g);
will have problems - root->val
isn't a bst<Item, Key> const *
. You likely mean f(root, g)
Upvotes: 4