ElFik
ElFik

Reputation: 1027

boost thread error <unresolved overloaded function type>

I'm working on an optimisation project and have decided to try threads to increase the speed of my code. the format of the code is:

Main.cpp:

int main(int argc, char **argv) {
    B *b = new B(argv[1]);
    b->foo();
    delete b;
    return EXIT_SUCCESS;
}

B.cpp:

#include B.hpp

B::B(const char *filename) { .... }

B::task1(){ /*nop*/ }

void B::foo() const { 
    boost::thread td(task1);
    td.join();
}

B.hpp:

#include <boost/thread.hpp>

class B{
    public:
    void task1();
    void foo();
}

however when I try to compile this code, I get an error at boost::thread td(task1), saying:

error: no matching function for call to 'boost::thread::thread(<unresolved overloaded function type>)'

not entirely sure what the problem is and I've tried hacking away to no success. Any help is appreciated!

edit: new error

B.o: In function 'B::b() const':
B.cpp:(.text+0x7eb): undefined reference to 'vtable for boost::detail::thread_data_base'
B.cpp:(.text+0x998): undefined reference to 'boost::thread::start_thread()'
B.cpp:(.text+0x9a2): undefined reference to 'boost::thread::join()'
B.cpp:(.text+0xa0b): undefined reference to 'boost::thread::~thread()'
B.cpp:(.text+0xb32): undefined reference to 'boost::thread::~thread()'
B.o: In function 'boost::detail::thread_data<boost::_bi::bind_t<void, boost::_mfi::cmf0<void, B>, boost::_bi::list1<boost::_bi::value<B const*> > > >::~thread_data()':
B.cpp:(.text._ZN5boost6detail11thread_dataINS_3_bi6bind_tIvNS_4_mfi4cmf0Iv4BEENS2_5list1INS2_5valueIPKS6_EEEEEEED2Ev[_ZN5boost6detail11thread_dataINS_3_bi6bind_tIvNS_4_mfi4cmf0Iv4BEENS2_5list1INS2_5valueIPKS6_EEEEEEED5Ev]+0x8): undefined reference to 'boost::detail::thread_data_base::~thread_data_base()'

Upvotes: 1

Views: 2311

Answers (1)

juanchopanza
juanchopanza

Reputation: 227370

B::task() is a member function, so it takes an implicit first parameter of type B*. So you need to pass an instance to it in order to use it in a boost::thread.

void B::foo() const { 
  boost::thread td(&B::task1, this); // this is a const B*: requires task1() to be const.
  td.join();
}

But since B::foo() is a const method, you would have to make B::task1() a const method too:

class B {
  void task1() const:
}

Upvotes: 3

Related Questions