c++ - boost::线程编译错误

标签 c++ boost-thread

我正在尝试使用 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/

相关文章:

作为另一个对象的一部分的对象的 C++ 构造函数

c++ - 在 visual studio(非 .net)中编译 "standard"C++

c++ - 将类对象作为参数传递给 boost::thread

c++ - 程序在一段时间后终止 "std::bad_alloc"

c++ - 在 main 中调用公共(public)函数

c++ - 用 str(const char*) 设置 std::stringstream 的内容会产生奇怪的后果

c++使用临时对象右值初始化左值引用以及自动推导

c++ - Boost 1.48.0 upgrade_to_unique_lock on Linux : Has something changed since 1. 47 还是我做错了什么?

c++ - 是否有可能在 C++ 中使用 std::atomic_flag 获得线程锁定机制?

c++ - 多线程效率不高 : Debugging False Sharing?