在调用传递给模板函数的函数时调用指向成员函数的指针
这是我尝试使用的提供的函数模板:
template <class Process, class BTNode>
void postorder(Process f, BTNode* node_ptr)
{
if (node_ptr != 0)
{
postorder( f, node_ptr->left() );
postorder( f, node_ptr->right() );
f( node_ptr->data() );
}
}
这是我的调用,以及我传递的函数:
void city_db::print_bst() {
postorder(&city_db::print, head);
}
void city_db::print(city_record target)
{
std::cout << target.get_code();
}
这是编译时间(G++ )我得到的错误:
CityDb.cpp:85:实例化自 这里
BinTree.template:80: 错误:必须使用 '.' 或 '->' 来调用 'f 中的成员函数指针 (...)'
制作:*** [CityDb.o] 错误 1
这是引用函数模板中的行 f( node_ptr->data() );
。
这是一个数据结构项目。分配被修改了,所以我们不需要将函数传递给函数,但我对此感兴趣已经有一段时间了,我觉得我几乎已经在这里了。我已经用尽了 Google 和实验室助教,所以如果 StackOverflow 有想法,他们将不胜感激。
This is the provided function template I'm trying to use:
template <class Process, class BTNode>
void postorder(Process f, BTNode* node_ptr)
{
if (node_ptr != 0)
{
postorder( f, node_ptr->left() );
postorder( f, node_ptr->right() );
f( node_ptr->data() );
}
}
This is my call, and the function I'm passing:
void city_db::print_bst() {
postorder(&city_db::print, head);
}
void city_db::print(city_record target)
{
std::cout << target.get_code();
}
This is the compile time (G++) error I get:
CityDb.cpp:85: instantiated from
hereBinTree.template:80: error: must use
‘.’ or ‘->’ to call
pointer-to-member function in ‘f
(...)’make: *** [CityDb.o] Error 1
This is in reference to the line f( node_ptr->data() );
in the function template.
This is for a Data Structures project. The assignment was modified so we don't need to pass a function to a function, but I've been interested in this for quite some time, and I feel like I almost have it here. I've exhausted Google and Lab TA's, so if StackOverflow has ideas, they would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的问题是 postorder 接受必须以这种方式调用的函数对象:
您正在传递一个指向成员函数的指针。您应该首先调用 mem_fun 从指向成员的指针创建一个函数对象:
返回的函数对象有两个参数:指向 city_db 的指针(隐式 this 指针)和要打印的对象。您可以使用 bind1st 将第一个绑定到此,如下所示:
现在您应该能够对其调用 postorder:
Your problem is that postorder accepts a function object that must be called this way:
You are passing in a pointer to member function. You should first call mem_fun to make a function object from the pointer to member:
The returned function object takes two arguments: the pointer to a city_db (the implicit this pointer), and the object to be printed. You can bind the first to this with bind1st, like this:
And now you should be able to call postorder on it:
您需要一个
city_db
实例来调用print
。您传递的是一个指向成员函数的指针(将其视为 vtable 中的一个槽),但您还需要一个
this
指针。您可以将其作为另一个参数传递给postorder
函数。请参阅 C++ FAQ Lite
You need an instance of
city_db
to callprint
on.What you're passing is a pointer to a member function (think of it as a slot in the vtable), but you need a
this
pointer too. You could pass this in as another argument to thepostorder
function.See C++ FAQ Lite
您需要将 city_db::print() 设为静态或提供 city _db 对象。
You need to either make city_db::print() static or provide a city _db object.
正如所写的
不依赖于类状态。将其声明为静态函数,编译器将不需要
this
指针来调用它。有关该主题的常见问题解答。
As written
Does not depend on the class state. Declare it as a static function and compiler will not need
this
pointer to call it.FAQ on the topic.