2013-06-01 46 views
0

我有以下問題。C++多線程類方法

vector<thread> vThreads; 

list<Crob *> lRobs; 
list<Crob *>::iterator i; 

for(i = lRobs.begin(); i != lRobs.end(); i++) 
{ 
    vThreads.push_back(thread((*i)->findPath)); 
} 

我想的方法findPath傳遞給一個線程,但我得到了很多的錯誤......

> labrob.cpp: In function ‘int main(int, char**)’: 
labrob.cpp:72:43: error: no matching function for call to ‘std::thread::thread(<unresolved overloaded function type>)’ 
labrob.cpp:72:43: note: candidates are: 
In file included from labrob.cpp:14:0: 
/usr/include/c++/4.7/thread:131:7: note: std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = int (Crob::*)(); _Args = {}] 
/usr/include/c++/4.7/thread:131:7: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘int (Crob::*&&)()’ 
/usr/include/c++/4.7/thread:126:5: note: std::thread::thread(std::thread&&) 
/usr/include/c++/4.7/thread:126:5: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘std::thread&&’ 
/usr/include/c++/4.7/thread:122:5: note: std::thread::thread() 
/usr/include/c++/4.7/thread:122:5: note: candidate expects 0 arguments, 1 provided 
make: *** [labrob.o] Error 1 

我已經嘗試通過本地功能和無的問題,工作。 .. 如果您需要額外的代碼片段,請說。

我希望有人能幫助我。

編輯:

#pragma once 

包括 「point.hpp」

包括 「lab.hpp」

類Crob { 保護: 口岸系統* POS機; int steps; Clab * labfind; 弦方向;

public: Crob(Clab * lab);虛擬〜Crob(); virtual void findPath(); void moveTo(int x,int y);
void moveToPrint(int x,int y); int getSteps(void); void checkDirection(); };

新增CRob頭

+1

'Crob'是什麼定義? –

+1

你應該使用'std :: bind()'將這個方法傳遞給線程。 –

回答

1

貌似你試圖傳遞一個非靜態方法到的std ::線程構造函數。你不能這樣做:一個非靜態方法需要一個對象,所以它可以被調用。看起來像這樣:

for(i = lRobs.begin(); i != lRobs.end(); i++) 
{ 
    vThreads.push_back(std::thread(&Crob::findPath, *i)); 
} 
+0

啊,非常感謝! 但我如何從我的列表中得到確切的對象? 或者我得到* i? 雖然我仍然得到一個錯誤... labrob.cpp:在函數'int main(int,char **)'中: labrob.cpp:72:35:error:invalid use of non-static member function 'virtual void Crob :: findPath()' make:*** [labrob.o]錯誤1 – user2443761

+0

對不起,我忘記調用構造函數 – Guillaume

+0

你是我的英雄! – user2443761