g ++使用模板参数提供“未解析的重载函数类型”

时间:2011-04-29 06:03:29

标签: c++ templates

简化为基本形式,我试图用二叉搜索树做这样的事情:

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);
}

当我调用print_paths_bst时,我得到了这个:error: no matching function for call to ‘preorder_processing1(const bst<int, int>*&, <unresolved overloaded function type>, const bst<int, int>*&)’

我尝试在preorder_processing1调用上强制执行强制转换,如

preorder_processing1(root, print_if_leaf<Item, Key>, root);

......但这也没有解决问题。

有人看到这有什么问题吗?

1 个答案:

答案 0 :(得分:4)

template <class Item, class Key, class Process, class Param>
void preorder_processing1(bst<Item, Key> *root, Process f, Param g)

这需要const bst<Item, Key> *

此外,您对f(root->val, g);的来电会出现问题 - root->val不是bst<Item, Key> const *。您可能意味着f(root, g)