提升线程错误<未解决的重载函数类型>

boost thread error <unresolved overloaded function type>

本文关键字:重载 函数 类型 gt 未解决 错误 lt 线程      更新时间:2023-10-16

我正在进行一个优化项目,并决定尝试使用线程来提高代码的速度。代码的格式为:

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();
}

然而,当我试图编译这段代码时,我在boost::thread td(task1)处得到一个错误,说:

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

我不完全确定是什么问题,我试过破解,但没有成功。任何帮助都是感激的!

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()'

B::task()是一个成员函数,因此它接受B*类型的隐式第一个参数。所以你需要传递一个实例给它,以便在boost::thread中使用它。

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

但由于B::foo()const方法,您将不得不使B::task1()也成为const方法:

class B {
  void task1() const:
}