0
votes

I am trying to run the following program using 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;
}

It is throwing an error while compiling as given below:

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)

I am using boost 1.42. I have also tried old style boost::thread creation.

When hello() is not a class function, everything goes fine. Please let me know how can I fix it?

3

3 Answers

2
votes

You didn't read the documentation.

You either need to make the hello method function static, or create thread by passing object of type test to it's constructor :

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

The problem is you are try to bind to a member function try the following (i don't have your boost version so have no idea if this works for sure)

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

Failing that you can use a binder

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

If your compiler is new enough you can use the standard library equivalents of all of those by changing the boost namespace for std:: (the placeholder are in std::placeholders not the global namespace).

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

Try with this code:

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