我正在尝试使用 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: 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)
我正在使用 boost 1.42。我也尝试过旧式的 boost::thread 创建。
当 hello() 不是类函数时,一切正常。请让我知道如何解决它?
最佳答案
您没有阅读 documentation .
您要么需要使 hello
方法函数静态化,要么通过将类型 test 的对象传递给它的构造函数来创建线程:
int main(int argc, char* argv[])
{
test t;
boost::thread thrd(&test::hello, &t, 10);
thrd2.join();
}
关于c++ - boost::线程编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9811009/