本文介绍了std :: bind,this和QtConcurrent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用 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< 1& const TuringMachine *,ConfigNode *)'
... / 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&)'
/ pre>

注意:预期有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);
}


解决方案

如果要绑定成员函数,你必须通过一个这个指针,在你的情况下,这将意味着你必须通过2 this -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 bind this to method that is used in QtConcurrent::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 each ConfigNode in currentConfigs. Im using std::bind to bind this to step so it only requires one argument, as in the documentation of blockingMapped.

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 2 this-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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 18:51