提升线程错误<未解决的重载函数类型>
boost thread error <unresolved overloaded function type>
我正在进行一个优化项目,并决定尝试使用线程来提高代码的速度。代码的格式为:
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:
}
相关文章:
- 为什么这个运算符<重载函数对 STL 算法不可见?
- 可以打印矢量和矢量中的矢量的非重载C++函数
- 错误 没有与参数列表匹配的重载函数"getline"实例
- 使用模板重载函数
- C++线程中,没有重载函数接受 X 参数
- std::vector 没有重载函数的实例与参数列表匹配
- C++重载函数,一个采用基类的参数,另一个采用派生类的参数
- 错误:无法解析对重载函数的引用;你的意思是调用它吗?
- 对重载函数find_first_not_of的不明确调用
- 如何从重载解析中删除重载函数?
- CUDA:重载函数"isnan"的多个实例
- C++派生类重载函数(带有 std::function 参数)不可见
- 避免在人为的重载函数调用中拼写出类型
- C++:如何为多个重载函数保留通用代码路径?
- 什么时候可以使用常量装饰调用我的重载函数?
- 尝试使用谓词函数会导致错误:"std::sort"未找到匹配的重载函数
- std::调用,未找到匹配的重载函数
- 为什么在传递长整型时调用具有两个双精度类型的参数的重载函数?
- 为什么使用不匹配的参数调用重载函数仍然有效
- 如何通过签名作为模板参数来解决重载函数?