[英]boost::thread compilation error
我正在嘗試使用boost :: thread運行以下程序。
#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;
}
如下所示,它在編譯時拋出錯誤:
thread.c:17:33:錯誤:沒有匹配的函數可以調用'boost :: thread :: thread(,int)'/usr/include/boost/thread/detail/thread.hpp:236:9:注意:候選者是:boost :: thread :: thread(F,A1)[with F = void(test :: *)(int),A1 = int] /usr/include/boost/thread/detail/thread.hpp:202 :9:注意:
boost :: thread :: thread(boost :: detail :: thread_move_t)
我正在使用Boost 1.42。 我也嘗試過舊式的boost :: thread創建。
當hello()不是類函數時,一切正常。 請讓我知道如何解決?
您沒有閱讀文檔 。
您要么需要使hello
方法函數靜態化,要么通過將test類型的對象傳遞給它的構造函數來創建線程:
int main(int argc, char* argv[])
{
test t;
boost::thread thrd(&test::hello, &t, 10);
thrd2.join();
}
問題是您正在嘗試綁定到成員函數,請嘗試以下操作(我沒有您的增強版本,所以不確定是否可以正常工作)
boost::thread thrd(&test::hello, &t, 10);
無法使用活頁夾
boost::thread thrd(
boost::bind(&test::hello, &t, 10));
如果您的編譯器足夠新,則可以通過更改std ::的boost命名空間來使用所有標准庫的等效庫(占位符位於std :: placeholders中,而不是全局命名空間中)。
std::thread(... //c++11
嘗試使用以下代碼:
int main(int argc, char* argv[])
{
test t;
boost::thread thrd(&test::hello,&t,10);
thrd.join();
return 0;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.