boost::bind 函数及其输入参数错误
我正在尝试通过 boost::bind
进行多线程处理。但是,我得到了错误:
src/model.cpp:225:从这里实例化
/boost_1_45_0v/include/boost/bind/mem_fn.hpp:333: 错误:指向成员类型void (Model::)(taskDataSPType&)
的指针与对象类型taskDataSPT1ype
不兼容
make: * [model.o] 错误 1
这是我的代码:
void Model::runTask(taskDataSPType& myTask)
{}
taskDataSPType myTask;
fifo_pool tp(thread_num);
for (int i = 0 ; i < totalTaskNum ; +i)
{
taskQueue.wait_and_pop(myTask, i);
myTask.taskID = i ;
// run the typical iterations
tp.schedule(boost::bind(&Model::runTask, &myTask));
}
tp.wait();
在另一个头文件中,我有:
typedef struct taskDataSPT1ype taskDataSPType;
struct taskDataSPT1ype
{
int taskID;
int startNode;
int endNode;
};
I am trying to do multithreading by boost::bind
. But, I got error:
src/model.cpp:225: instantiated from here
/boost_1_45_0v/include/boost/bind/mem_fn.hpp:333: error: pointer to member typevoid (Model::)(taskDataSPType&)
incompatible with object typetaskDataSPT1ype
make: * [model.o] Error 1
Here is my code:
void Model::runTask(taskDataSPType& myTask)
{}
taskDataSPType myTask;
fifo_pool tp(thread_num);
for (int i = 0 ; i < totalTaskNum ; +i)
{
taskQueue.wait_and_pop(myTask, i);
myTask.taskID = i ;
// run the typical iterations
tp.schedule(boost::bind(&Model::runTask, &myTask));
}
tp.wait();
In another header file, I have :
typedef struct taskDataSPT1ype taskDataSPType;
struct taskDataSPT1ype
{
int taskID;
int startNode;
int endNode;
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Model::runTask
(大概)是一个非静态成员函数。这意味着如果没有类的实例,您无法调用它。 boost::bind 知道这一点,因此它期望第一个参数是某种形式的 Model 或其派生类。因此,您的bind
需要两个参数:Model
和函数参数taskDataSPType&
。另外,你的参数是一个引用,但你似乎附加了一个指针。那也是行不通的。您可能需要使用
boost::ref
,如下所示:Model::runTask
is (presumably) a non-static member function. That means you cannot call it without an instance of the class.boost::bind
knows this, and therefore it expects the first parameter to be aModel
of some form, or a derived class thereof. So yourbind
takes two parameters: theModel
and the function argumenttaskDataSPType&
.Also, your argument is a reference, but you seem to be attaching a pointer. That's not going to work either. You may need to use
boost::ref
, as follows:&Model::runTask
是一个成员函数,因此它有一个额外的隐式参数this
。因此,在您的特定情况下,您希望将其与两个参数绑定:Model
的实例和taskDataSPType
对象。请注意,如果想要使用bind
传递引用,则必须使用boost::ref
或boost::cref
。&Model::runTask
is a member function, and as such it has an extra implicit argumentthis
. So in your particular case, you want to bind it with two arguments: an instance ofModel
and ataskDataSPType
object. Note that if one wants to pass references withbind
it has to useboost::ref
orboost::cref
.