2011-04-29 72 views
0

減少到基本形式,我試圖做這樣的事情有二叉搜索樹:克++是給「未解決重載函數型」與模板參數

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

...但是這還沒有解決這個問題。

有沒有人看到這有什麼問題?

回答

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)

+0

我做了您所建議的更改。我錯過了f(根,g)的東西,好的電話。然而,我仍然收到相同的編譯錯誤,這次用const:error:沒有匹配函數調用'preorder_processing2(const bst *&,<未解析的重載函數類型>,const bst *&)' – deeb 2011-04-29 06:17:40

+0

您你需要你的'print_if_leaf '變種。 – Erik 2011-04-29 06:19:58

+0

對不起,現在已經很晚了,我一直在努力。你究竟是什麼意思,我需要我的print_if_leaf 變種? – deeb 2011-04-29 06:24:46

相關問題