gpt4 book ai didi

c++ - Boost 线程示例显示错误,它无法匹配调用 'boost::thread::thread()' 的函数

转载 作者:行者123 更新时间:2023-11-30 04:33:39 27 4
gpt4 key购买 nike

我在创建 namspace multithread_init 时在 main 函数上初始化线程,以便将 set_multihthread 类推送到命名空间。为什么在声明 boost::thread 之后它没有匹配调用 boost::thread t(smulti.thread) 的函数?

#define BOOST_THREAD_USE_LIB
#include <boost/thread.hpp>
#include <boost/thread/thread.hpp>
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <iostream>


#ifndef MULTITHREAD_INIT_HPP_
#define MULTITHREAD_INIT_HPP_
namespace multithread_init{
class set_multithread{
private:
//t;
public:
void thread(){
for(int i = 0; i < 5; i++){
wait(1);
std::cout<<" thread i value : "<<i<<std::endl;
}
}

void wait(int seconds)
{
boost::this_thread::sleep(boost::posix_time::seconds(seconds));
}

// void multi_case(){
// t.join();
// boost::thread t(thread);
// }

};
}

#endif /* MULTITHREAD_INIT_HPP_ */

主要文件如下。

int main()
{

/*thread */
multithread_init::set_multithread smulti;
boost::thread t(smulti.thread);
t.join();

}

最佳答案

您不能以这种方式传递成员函数。你需要将它绑定(bind)到对象上

boost::thread  t(boost::bind(&multithread_init::set_multithread::thread, &smulti));

关于c++ - Boost 线程示例显示错误,它无法匹配调用 'boost::thread::thread(<unresolved overloaded function type>)' 的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6648725/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com