【问题标题】:boost thread error <unresolved overloaded function type>boost线程错误<未解决的重载函数类型>
【发布时间】:2013-02-20 10:00:11
【问题描述】:

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

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(&lt;unresolved overloaded function type&gt;)'

不完全确定问题出在哪里,我尝试破解但没有成功。任何帮助表示赞赏!

编辑:新错误

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

【问题讨论】:

    标签: c++ multithreading boost


    【解决方案1】:

    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:
    }
    

    【讨论】:

    • 在简化我的代码时,我可能过于简化了——我的B 类有一个需要在task1() 中使用的成员变量。创建一个新的 B 并与之绑定会阻止我使用 objects 变量,不是吗?
    • @ElFik 该示例仅用于说明。您可以通过this 而不是&amp;bthisB*
    • @ElFik 我将示例更改为从B 的方法之一中启动线程。
    • 感谢您的帮助,但我的 const 声明似乎有问题。我尝试通过B b = this; 进行复制,但出现错误:invalid conversion from 'const Mesh* const' to Mesh*
    • @ElFik foo() 是一个 const 方法,所以它不能修改 this。所以你不能从中调用任何非常量方法。这意味着this 在该方法中是const。您必须使 foo() 非 const 或 task1() const。
    猜你喜欢
    • 1970-01-01
    • 2013-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多