簡體   English   中英

g++ 給出帶有模板參數的“未解決的重載 function 類型”

[英]g++ is giving “unresolved overloaded function type” with template parameters

簡化為基本形式,我試圖用二叉搜索樹做這樣的事情:

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

...但這也沒有解決問題。

有誰看到這有什么問題?

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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM