g++给出“未解析的重载函数类型”带模板参数
简化为基本形式,我尝试使用二叉搜索树执行类似的操作:
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: nomatching function for call to 'preorder_processing1(const bst
我已经尝试过强制对 preorder_processing1 调用进行强制转换,例如
preorder_processing1(root, print_if_leaf<Item, Key>, root);
...但这也没有解决问题。
有人看出这有什么问题吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这需要采用 const bst- 。 *
此外,您对
f(root->val, g);
的调用将会出现问题 -root->val
不是>bst<项目,键>常量*
。您可能是指f(root, 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 abst<Item, Key> const *
. You likely meanf(root, g)