【发布时间】:2016-09-23 21:21:46
【问题描述】:
我试图将一个类的模板化成员函数作为另一个类的模板化成员函数的参数传递。我见过几个使用函数指针的例子,但我试图直接传递这个函数参数。
在
template <class Item>
class MinHeap
我有这个功能
tempmlate <class Item>
template <class Process>
void inorder (Process f, const int index)
{
if (index < size())
{
inorder(f, index*2 +1);
f(data[index]);
inorder(f, index*2 +2);
}
}
在
template<item>
class sequ
我有一个函数叫做
void insert(const Item& x);
我正在尝试在 main 中执行此操作:
MinHeap<int>* tree = new MinHeap<int>();
//insert some stuff
sequ<int>* s = new sequ<int>();
tree->inorder(s->insert);
但最后一行给了我错误:
error: reference to non-static member function must be called
tree->inorder(s->insert);
当我用函数替换 s->insert 时,打印
void print(int x)
{
printf("%d\n", x);
}
效果很好。
如何使用成员函数作为参数?
【问题讨论】:
标签: c++ function class templates methods