2012-11-30 44 views
3

林欲以std::bind結合this到方法,該方法在QtConcurrent::blockingMapped的std ::綁定,這和QtConcurrent

部首使用:

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

那麼,我在這裏做什麼是運行步驟conncurre blockingMapped每個ConfigNodecurrentConfigs。 Im使用std::bind綁定thisstep,因此它只需要一個參數,如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); 
} 

回答

3

如果你要綁定一個成員函數,你將不得不通過一個this指針,而你的情況就意味着,你將不得不通過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); 

不理解你的代碼,我可能會重新調整你的代碼,這樣你就可以避免雙重指針this

+0

爲什麼需要第二個'this'? – TeaOverflow

+0

@Evgeni首先這是調用成員函數所必需的,因爲這樣做時,第二個參數需要是指向您的成員函數所源自的類的指針或您的類的對象。你可以例如說'std :: bind(&foo :: bar,bar())'。 – inf

+0

那麼,我基本上試圖在幾個'ConfigNode'對象上同時運行'step'。 'blockingMapped'似乎是一個好主意,因爲它就是這樣做的。但我不能使用成員函數,如[這裏]所示(http://doc.qt.digia.com/4.7-snapshot/qtconcurrentmap.html#using-bound-function-arguments)。我必須綁定它或使其成爲靜態/全局。 – TeaOverflow