2012-03-21 122 views
0

我想使用boost :: thread運行下面的程序。boost ::線程編譯錯誤

thread.c:17:33: error: no matching function for call to 'boost::thread::thread(, int)' /usr/include/boost/thread/detail/thread.hpp:236:9: note: candidates are: boost::thread::thread(F, A1) [with F = void (test::*)(int), A1 = int] /usr/include/boost/thread/detail/thread.hpp:202:9: note:
boost::thread::thread(boost::detail::thread_move_t)

我使用升壓1.42:

#include <boost/thread.hpp> 
#include <iostream> 

using namespace std; 

class test{ 
public: 
    void hello(int i) 
    { 
    cout << i << " "; 
    }; 
}; 

int main(int argc, char* argv[]) 
{ 
    class test t; 
    boost::thread thrd(t.hello, 10); 
    thrd.join(); 
    return 0; 
} 

在編譯如下給出它拋出一個錯誤。我也嘗試過老式的boost :: thread創建。

打招呼時()是不是一類的功能,一切順利。請讓我知道我該如何修復它?

回答

1

的問題是您嘗試綁定到一個成員函數嘗試以下(我沒有你提升版本,所以不知道這是否是肯定的)

boost::thread thrd(&test::hello, &t, 10); 

如果做不到這一點,你可以用粘合劑

boost::thread thrd(
    boost::bind(&test::hello, &t, 10)); 

如果你的編譯器足夠新,你可以通過改變升壓命名空間的std ::使用所有這些標準庫當量(佔位符是的std ::佔位符不全局命名空間)。

std::thread(... //c++11 
+0

第一個建議工作...感謝您的答案。 – 2012-03-21 19:16:22

+0

@AkashAgrawal是的,它在我的工作方式,現在只是1.42歲,很高興爲服務兄弟。 – 111111 2012-03-21 19:18:00

2

你沒有讀過documentation

你要麼需要進行hello方法的功能的靜態,或傳遞類型的測試對象給它的構造函數創建線程:

int main(int argc, char* argv[]) 
{ 
    test t; 
    boost::thread thrd(&test::hello, &t, 10); 
    thrd2.join(); 
} 
+0

是的,我有靜態函數功能測試它張貼問題之前,他們顯然會工作。無法使用非靜態函數運行。謝謝你的回答,會試試這個。 – 2012-03-21 19:13:30

0

嘗試使用此代碼:

int main(int argc, char* argv[]) 
{ 
    test t; 
    boost::thread thrd(&test::hello,&t,10); 
    thrd.join(); 
    return 0; 
}