问题描述
我试图使用 std :: bind
将绑定到<$ c $中使用的方法c> QtConcurrent :: blockingMapped
标题:
class TuringMachine
{
private:
TRTable table;
std :: set< ConfigNode *> currentConfigs;
//函数对象
std :: function< std :: set< ConfigNode *>(const TuringMachine *,ConfigNode *)> step_f;
//方法它将保存
std :: set< ConfigNode *> step(TuringMachine * this_m,ConfigNode * parent);
std :: set< ConfigNode *>& makeStep();
}
>
TuringMachine :: TuringMachine(/ ** /)
{
step_f = std :: bind (& TuringMachine :: step,this,std :: placeholders :: _ 1);
}
std :: set< ConfigNode *> & TuringMachine :: makeStep(){
auto configSet = QtConcurrent :: blockingMapped< QLinkedList< std :: set< ConfigNode *>>(currentConfigs,step_f); //并发执行!
/ ** /
return currentConfigs;
}
std :: set< ConfigNode *> TuringMachine :: step(TuringMachine * this_m,ConfigNode * parent){//实际步骤
/ ** /
}
所以,我在这里做每个配置节点
>在 currentConfigs
中。 Im使用 std :: bind
将绑定到
到
步骤
所以它只需要一个参数,如在 blockingMapped
的文档中。
获取
错误: std :: _Bind< std :: _ Mem_fn< std :: set< ConfigNode *>(TuringMachine :: *)(TuringMachine *,ConfigNode *)>(TuringMachine *,std :: _ Placeholder& const TuringMachine *,ConfigNode *)'
/ pre>
... / Qt / 474 / gcc / include / QtCore / qtconcurrentmapkernel.h:121:错误:没有匹配的调用'(std :: function< std :: set< ; ConfigNode *>(const TuringMachine *,ConfigNode *)>)(ConfigNode * const&)'
... / Qt / 474 / gcc / include / QtCore / qtconcurrentmapkernel.h:136:error:no匹配以调用'(std :: function< std :: set< ConfigNode *>(const TuringMachine *,ConfigNode *)>)(ConfigNode * const&)'
和
注意:预期有2个参数,提供1个参数
$ b b我错了什么?
EDIT
工作版本(供未来参考):
标题:
// function object
std :: function< std :: set< ConfigNode *>(ConfigNode *)> step_f;
//方法它将保存
std :: set< ConfigNode *> step(ConfigNode * parent);
资料来源:
code> TuringMachine :: TuringMachine(/ ** /)
{
step_f = std :: bind(& TuringMachine :: step,this,std :: placeholders :: _ 1);
}
解决方案如果要绑定成员函数,你必须通过一个
这个
指针,在你的情况下,这将意味着你必须通过2this
-pointers:
正常调用成员函数:
struct bar {
int a;
void foo(){
std :: cout<< a<< std :: endl;
}
void call_yourself(){
auto f = std :: bind(& bar :: foo,this);
f();
}
};
您的情况:
step_f = std :: bind(& TuringMachine :: step,this,this,std :: placeholders :: _ 1);
没有理解你的代码,我可能会重新编写你的代码, $ c> this 指针。
Im trying to use
std::bind
to bindthis
to method that is used inQtConcurrent::blockingMapped
Header:
class TuringMachine { private: TRTable table; std::set<ConfigNode*> currentConfigs; //function object std::function<std::set<ConfigNode*>( const TuringMachine*, ConfigNode*)> step_f; //method it will hold std::set<ConfigNode *> step(TuringMachine* this_m ,ConfigNode *parent); std::set<ConfigNode*>& makeStep();
}
Source:
TuringMachine::TuringMachine(/**/) { step_f = std::bind(&TuringMachine::step, this, std::placeholders::_1); } std::set<ConfigNode*> &TuringMachine::makeStep(){ auto configSet = QtConcurrent::blockingMapped<QLinkedList<std::set<ConfigNode*>>>(currentConfigs, step_f);//concurrent execution! /**/ return currentConfigs; } std::set<ConfigNode*> TuringMachine::step(TuringMachine *this_m, ConfigNode * parent){ //the actual step /**/ }
So what Im doing here is running step conncurrently with
blockingMapped
on eachConfigNode
incurrentConfigs
. Im usingstd::bind
to bindthis
tostep
so it only requires one argument, as in the documentation ofblockingMapped
.Im getting
error: no match for call to '(std::_Bind<std::_Mem_fn<std::set<ConfigNode*> (TuringMachine::*)(TuringMachine*, ConfigNode*)>(TuringMachine*, std::_Placeholder<1>)>) (const TuringMachine*, ConfigNode*)' .../Qt/474/gcc/include/QtCore/qtconcurrentmapkernel.h:121: error: no match for call to '(std::function<std::set<ConfigNode*>(const TuringMachine*, ConfigNode*)>) (ConfigNode* const&)' .../Qt/474/gcc/include/QtCore/qtconcurrentmapkernel.h:136: error: no match for call to '(std::function<std::set<ConfigNode*>(const TuringMachine*, ConfigNode*)>) (ConfigNode* const&)'
And
note: 2 arguments expected, 1 provided
Where did I go wrong?
EDIT
Corrected, working version (for future "reference"):
Header:
//function object std::function<std::set<ConfigNode*>( ConfigNode*)> step_f; //method it will hold std::set<ConfigNode *> step(ConfigNode *parent);
Source:
TuringMachine::TuringMachine(/**/) { step_f = std::bind(&TuringMachine::step, this, std::placeholders::_1); }
解决方案If you want to bind a member function, you would have to pass a
this
pointer, which in your case would mean that you would have to pass 2this
-pointers:Normal call to member function:
struct bar { int a; void foo() { std::cout << a << std::endl; } void call_yourself() { auto f = std::bind(&bar::foo, this); f(); } };
Your case:
step_f = std::bind(&TuringMachine::step, this, this,std::placeholders::_1);
Without understanding your code, I would probably redesing your code such that you can avoid the double
this
pointer.这篇关于std :: bind,this和QtConcurrent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!