boost::线程编译错误
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)[其中F=void(test::*)(int),A1=int]/usr/include/boost/thread/detail/thread.hpp:202:9:注意:
boost::thread::thread(boost::detail::thread_move_t)
我使用的是助推1.42。我也尝试过老式的boost::线程创建。
当hello()不是类函数时,一切都很好。请告诉我该怎么修?
您没有阅读文档。
您要么需要使hello
方法函数为静态的,要么通过将类型测试的对象传递给它的构造函数来创建线程:
int main(int argc, char* argv[])
{
test t;
boost::thread thrd(&test::hello, &t, 10);
thrd2.join();
}
问题是你试图绑定到一个成员函数,请尝试以下操作(我没有你的boost版本,所以不知道这是否有效)
boost::thread thrd(&test::hello, &t, 10);
如果失败,您可以使用活页夹
boost::thread thrd(
boost::bind(&test::hello, &t, 10));
如果你的编译器足够新,你可以通过更改std::的boost命名空间来使用所有这些的标准库等价物(占位符在std::占位符中,而不是全局命名空间)。
std::thread(... //c++11
尝试使用以下代码:
int main(int argc, char* argv[])
{
test t;
boost::thread thrd(&test::hello,&t,10);
thrd.join();
return 0;
}
相关文章:
- 用MacOS Mojave编译C++:致命错误:mpi.h:没有这样的文件或目录
- std::is_base_of表示ctor编译错误
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- 在没有定义返回类型的函数中返回布尔值,并将结果保存在无错误的char编译中-为什么
- Qt5:使用QCommandLineParser类时出现奇怪的编译错误
- Qt Cmake 错误编译"GuiSupportQt not found"
- Opengl 精度转换错误编译错误 E0415
- 库将ARM架构错误编译为架构X64
- RT 音频 Mac 错误 g++ 编译错误
- 错误编译Boost.log
- 错误编译QT创建者 / QT窗口小部件示例
- 错误编译MIPS32
- Visual Studio 2013 中的错误(编译和运行代码)
- 链接错误编译qt项目在visual 2010
- 无法用模板错误编译nsgmls
- 奇怪的错误.编译失败
- 如果有人调用c++中的方法,则强制错误(编译时)
- 来自autoconf测试的错误编译命令
- 时间限制超出错误C++编译
- SFML 2.3 和 CodeBlocks 错误编译