c++ - 将模板函数传递给另一个模板函数

因此,我有一个函数,即预订处理,用于在bst的每个节点中的每个项目上执行函数f。功能如下:

template <class Item, class Key, class Process>
void preorder_processing(bstNode<Item, Key>*& root, Process f)
{
    if (root == NULL) return; 
    f(root);
    preorder_processing(root->left(), f); 
    preorder_processing(root->right(), f);
}

不幸的是,当我从main函数中调用该类时,出现错误。调用是preorder_processing(root_ptr,print);实际功能“打印”为:
template<class Item> 
void print(Item a) 
{
    cout << a << endl;
}

错误是:

bstNode.cxx:23: error: no matching function for call to ‘preorder_processing(bstNode<int, long unsigned int>* <unresolved overloaded function type>)



有人知道发生了什么吗?

最佳答案

您的root->left()root->right()应该返回的bstNode<Item, Key>*右值指针。您不能将非常量引用分配给临时指针变量。

如下更改声明,编译器错误应该出现:

void preorder_processing(bstNode<Item, Key>* root, Process f)
                 //    removed reference  ^^^

此外,调用该函数时,第二个参数Process f不会传递任何值:
preorder_processing(root->left(), ???); 

https://stackoverflow.com/questions/10442735/

相关文章:

iphone - 如何解决iPhone应用程序hpple HTML解析 'libxml/tree.h

compiler-errors - 添加平移和缩放错误

visual-studio-2010 - VS2010 Express 中的 Boost - 重新定

c# - 添加方法导致编译错误 C# ASP

testing - Flex Builder : Asset file or directory n

apache-flex - 类mx.core::DesignLayer could not be f

ruby - 为什么在编译ruby-1.9.3-p125时出现clang错误?

c# - 将Gridview导出到Excel文件-错误

c++ - 在UNIX上 “internal compiler error: storage fai

xml - 构建SLN : CS0008 - CS0115的奇怪错误