3

我正在尝试使用std::bind绑定this到中使用的方法QtConcurrent::blockingMapped

标题:

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();

}

资源:

    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
  /**/
}

blockingMapped所以我在这里做的是在每个ConfigNodein上同时运行 step currentConfigs。我使用std::bind绑定this到,step所以它只需要一个参数,如blockingMapped.

我越来越

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&)'

note: 2 arguments expected, 1 provided

我哪里做错了?

编辑

更正的工作版本(供将来“参考”):

标题:

    //function object
    std::function<std::set<ConfigNode*>( ConfigNode*)> step_f;
    //method it will hold
    std::set<ConfigNode *> step(ConfigNode *parent);

资源:

    TuringMachine::TuringMachine(/**/)  
{
    step_f = std::bind(&TuringMachine::step, this, std::placeholders::_1);
}
4

1 回答 1

3

如果要绑定成员函数,则必须传递一个this指针,在您的情况下,这意味着您必须传递 2 个this指针:

正常调用成员函数:

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);

在不了解您的代码的情况下,我可能会重新设计您的代码,以便您可以避免双this指针。

于 2012-11-30T14:46:41.150 回答