C++ boost::bind、boost::asio、boost::thread和类
这是我在类'sau_timer'的构造函数中的代码(它希望在单独的线程中运行一个计时器,然后调用另一个函数) 不幸的是,当我尝试编译时,我得到以下错误: 1> c:\program files\boost\boost\u 1\u 39\boost\bind\bind.hpp(246):错误C2064:术语不计算为带1个参数的函数C++ boost::bind、boost::asio、boost::thread和类,c++,multithreading,boost,bind,boost-asio,C++,Multithreading,Boost,Bind,Boost Asio,这是我在类'sau_timer'的构造函数中的代码(它希望在单独的线程中运行一个计时器,然后调用另一个函数) 不幸的是,当我尝试编译时,我得到以下错误: 1> c:\program files\boost\boost\u 1\u 39\boost\bind\bind.hpp(246):错误C2064:术语不计算为带1个参数的函数 还有一大堆警告。我做错了什么?我已经尝试了我所能想到的一切,谢谢。每个非静态成员函数都有一个第一个隐藏参数—调用函数的实例。因此,exec函数需要两个参数。您有适当的代
还有一大堆警告。我做错了什么?我已经尝试了我所能想到的一切,谢谢。每个非静态成员函数都有一个第一个隐藏参数—调用函数的实例。因此,exec函数需要两个参数。您有适当的代码,但它被注释掉了。我的意思是:
sau_timer::sau_timer(int secs, timerparam f) : strnd(io),
t(io, boost::posix_time::seconds(secs))
{
assert(secs > 0);
this->f = f;
//t.async_wait(boost::bind(&sau_timer::exec, this, _1));
t.async_wait(strnd.wrap(boost::bind(&sau_timer::exec, this)));
boost::thread thrd(&io,this);
io.run();
//thrd(&sau_timer::start_timer);
}
您是否尝试过它,但遇到了一些其他问题?每个非静态成员函数都有一个第一个隐藏参数,即要调用函数的实例。因此,exec函数需要两个参数。您有适当的代码,但它被注释掉了。我的意思是:
sau_timer::sau_timer(int secs, timerparam f) : strnd(io),
t(io, boost::posix_time::seconds(secs))
{
assert(secs > 0);
this->f = f;
//t.async_wait(boost::bind(&sau_timer::exec, this, _1));
t.async_wait(strnd.wrap(boost::bind(&sau_timer::exec, this)));
boost::thread thrd(&io,this);
io.run();
//thrd(&sau_timer::start_timer);
}
您是否尝试过它,但遇到了一些其他问题?我还需要它与strnd.wrap()一起使用。我再次将其更改为:
t.async_wait(boost::bind(&sau_timer::exec, this, _1));
但现在我发现了这些错误:
sau_timer::sau_timer(int secs, timerparam f) : strnd(io),
t(io, boost::posix_time::seconds(secs))
{
assert(secs > 0);
this->f = f;
t.async_wait(strnd.wrap(boost::bind(&sau_timer::exec, this, _1)));
boost::thread thrd(&io);
io.run();
}
void sau_timer::exec(const boost::system::error_code&) { (f)(params); }
1>----构建已启动:项目:Sauria,配置:调试Win32------
1> 编译。。。
1> sau_timer.cpp
1> 请适当地定义_WIN32\u WINNT或_WIN32\u窗口。例如:
1> -将-D_WIN32_WINNT=0x0501添加到编译器命令行;或
1> -将_WIN32_WINNT=0x0501添加到项目的预处理器定义中。
1> 假设_WIN32_WINNT=0x0501(即Windows XP目标)。
1> c:\program files\boost\boost\u 1\u 39\boost\bind\bind.hpp(246):错误C2064:术语不计算为带1个参数的函数
1> c:\ProgramFiles\boost\boost\u 1\u 39\boost\bind\bind\u template.hpp(20):请参阅正在编译的函数模板实例化“void boost::_bi::list1::operator()(boost::_bi::type,F&,A&,int)”的参考
1> 与
1> [
1> A1=升压::bi::值,
1> F=boost::asio::io_服务*,
1> T=无效,
1> A=boost::_bi::list0
1> ]
1> c:\ProgramFiles\boost\boost\u 1\u 39\boost\bind\bind\u template.hpp(18):编译类模板成员函数“void boost::\u bi::bind\u t::operator()(void)”时
1> 与
1> [
1> R=无效,
1> F=boost::asio::io_服务*,
1> L=boost::_bi::列表1
1> ]
1> c:\ProgramFiles\boost\boost\u 1\u 39\boost\thread\detail\thread.hpp(227):请参阅正在编译的类模板实例化“boost::\u bi::bind\u t”的参考
1> 与
1> [
1> R=无效,
1> F=boost::asio::io_服务*,
1> L=boost::_bi::列表1
1> ]
1> c:\users\ben\documents\visualstudio 2008\projects\sauria\sauria\sau_timer.cpp(11):请参阅正在编译的函数模板实例化“boost::thread::thread(F,A1)”的参考
1> 与
1> [
1> F=boost::asio::io_服务*,
1> A1=sau定时器*
1> ]
1> 生成日志保存在“file://c:\Users\Ben\Documents\visualstudio 2008\Projects\Sauria\Sauria\Debug\BuildLog.htm”中
1> Sauria-1个错误,0个警告
=========生成:0成功,1失败,0最新,0跳过========我还需要将其与strnd.wrap()一起使用。我再次将其更改为:
t.async_wait(boost::bind(&sau_timer::exec, this, _1));
但现在我发现了这些错误:
sau_timer::sau_timer(int secs, timerparam f) : strnd(io),
t(io, boost::posix_time::seconds(secs))
{
assert(secs > 0);
this->f = f;
t.async_wait(strnd.wrap(boost::bind(&sau_timer::exec, this, _1)));
boost::thread thrd(&io);
io.run();
}
void sau_timer::exec(const boost::system::error_code&) { (f)(params); }
1>----构建已启动:项目:Sauria,配置:调试Win32------
1> 编译。。。
1> sau_timer.cpp
1> 请适当地定义_WIN32\u WINNT或_WIN32\u窗口。例如:
1> -将-D_WIN32_WINNT=0x0501添加到编译器命令行;或
1> -将_WIN32_WINNT=0x0501添加到项目的预处理器定义中。
1> 假设_WIN32_WINNT=0x0501(即Windows XP目标)。
1> c:\program files\boost\boost\u 1\u 39\boost\bind\bind.hpp(246):错误C2064:术语不计算为带1个参数的函数
1> c:\ProgramFiles\boost\boost\u 1\u 39\boost\bind\bind\u template.hpp(20):请参阅正在编译的函数模板实例化“void boost::_bi::list1::operator()(boost::_bi::type,F&,A&,int)”的参考
1> 与
1> [
1> A1=升压::bi::值,
1> F=boost::asio::io_服务*,
1> T=无效,
1> A=boost::_bi::list0
1> ]
1> c:\ProgramFiles\boost\boost\u 1\u 39\boost\bind\bind\u template.hpp(18):编译类模板成员函数“void boost::\u bi::bind\u t::operator()(void)”时
1> 与
1> [
1> R=无效,
1> F=boost::asio::io_服务*,
1> L=boost::_bi::列表1
1> ]
1> c:\ProgramFiles\boost\boost\u 1\u 39\boost\thread\detail\thread.hpp(227):请参阅正在编译的类模板实例化“boost::\u bi::bind\u t”的参考
1> 与
1> [
1> R=无效,
1> F=boost::asio::io_服务*,
1> L=boost::_bi::列表1
1> ]
1> c:\users\ben\documents\visualstudio 2008\projects\sauria\sauria\sau_timer.cpp(11):请参阅正在编译的函数模板实例化“boost::thread::thread(F,A1)”的参考
1> 与
1> [
1> F=boost::asio::io_服务*,
1> A1=sau定时器*
1> ]
1> 生成日志保存在“file://c:\Users\Ben\Documents\visualstudio 2008\Projects\Sauria\Sauria\Debug\BuildLog.htm”中
1> Sauria-1个错误,0个警告
============生成:0成功,1失败,0最新,0跳过==============错误消息末尾有解释:
1>------ Build started: Project: Sauria, Configuration: Debug Win32 ------
1>Compiling...
1>sau_timer.cpp
1>Please define _WIN32_WINNT or _WIN32_WINDOWS appropriately. For example:
1>- add -D_WIN32_WINNT=0x0501 to the compiler command line; or
1>- add _WIN32_WINNT=0x0501 to your project's Preprocessor Definitions.
1>Assuming _WIN32_WINNT=0x0501 (i.e. Windows XP target).
1>c:\program files\boost\boost_1_39\boost\bind\bind.hpp(246) : error C2064: term does not evaluate to a function taking 1 arguments
1> c:\program files\boost\boost_1_39\boost\bind\bind_template.hpp(20) : see reference to function template instantiation 'void boost::_bi::list1<A1>::operator ()<F,boost::_bi::list0>(boost::_bi::type<T>,F &,A &,int)' being compiled
1> with
1> [
1> A1=boost::_bi::value<sau_timer *>,
1> F=boost::asio::io_service *,
1> T=void,
1> A=boost::_bi::list0
1> ]
1> c:\program files\boost\boost_1_39\boost\bind\bind_template.hpp(18) : while compiling class template member function 'void boost::_bi::bind_t<R,F,L>::operator ()(void)'
1> with
1> [
1> R=void,
1> F=boost::asio::io_service *,
1> L=boost::_bi::list1<boost::_bi::value<sau_timer *>>
1> ]
1> c:\program files\boost\boost_1_39\boost\thread\detail\thread.hpp(227) : see reference to class template instantiation 'boost::_bi::bind_t<R,F,L>' being compiled
1> with
1> [
1> R=void,
1> F=boost::asio::io_service *,
1> L=boost::_bi::list1<boost::_bi::value<sau_timer *>>
1> ]
1> c:\users\ben\documents\visual studio 2008\projects\sauria\sauria\sau_timer.cpp(11) : see reference to function template instantiation 'boost::thread::thread<boost::asio::io_service*,sau_timer*>(F,A1)' being compiled
1> with
1> [
1> F=boost::asio::io_service *,
1> A1=sau_timer *
1> ]
1>Build log was saved at "file://c:\Users\Ben\Documents\Visual Studio 2008\Projects\Sauria\Sauria\Debug\BuildLog.htm"
1>Sauria - 1 error(s), 0 warning(s)
如果您使用的是相对较新版本的Boost,我相信线程的ctor有一个方便的重载来处理bind(),允许简单地编写:
boost::thread thrd(boost::bind(&io_service::run, &io));
解释在错误消息的末尾:
1>------ Build started: Project: Sauria, Configuration: Debug Win32 ------
1>Compiling...
1>sau_timer.cpp
1>Please define _WIN32_WINNT or _WIN32_WINDOWS appropriately. For example:
1>- add -D_WIN32_WINNT=0x0501 to the compiler command line; or
1>- add _WIN32_WINNT=0x0501 to your project's Preprocessor Definitions.
1>Assuming _WIN32_WINNT=0x0501 (i.e. Windows XP target).
1>c:\program files\boost\boost_1_39\boost\bind\bind.hpp(246) : error C2064: term does not evaluate to a function taking 1 arguments
1> c:\program files\boost\boost_1_39\boost\bind\bind_template.hpp(20) : see reference to function template instantiation 'void boost::_bi::list1<A1>::operator ()<F,boost::_bi::list0>(boost::_bi::type<T>,F &,A &,int)' being compiled
1> with
1> [
1> A1=boost::_bi::value<sau_timer *>,
1> F=boost::asio::io_service *,
1> T=void,
1> A=boost::_bi::list0
1> ]
1> c:\program files\boost\boost_1_39\boost\bind\bind_template.hpp(18) : while compiling class template member function 'void boost::_bi::bind_t<R,F,L>::operator ()(void)'
1> with
1> [
1> R=void,
1> F=boost::asio::io_service *,
1> L=boost::_bi::list1<boost::_bi::value<sau_timer *>>
1> ]
1> c:\program files\boost\boost_1_39\boost\thread\detail\thread.hpp(227) : see reference to class template instantiation 'boost::_bi::bind_t<R,F,L>' being compiled
1> with
1> [
1> R=void,
1> F=boost::asio::io_service *,
1> L=boost::_bi::list1<boost::_bi::value<sau_timer *>>
1> ]
1> c:\users\ben\documents\visual studio 2008\projects\sauria\sauria\sau_timer.cpp(11) : see reference to function template instantiation 'boost::thread::thread<boost::asio::io_service*,sau_timer*>(F,A1)' being compiled
1> with
1> [
1> F=boost::asio::io_service *,
1> A1=sau_timer *
1> ]
1>Build log was saved at "file://c:\Users\Ben\Documents\Visual Studio 2008\Projects\Sauria\Sauria\Debug\BuildLog.htm"
1>Sauria - 1 error(s), 0 warning(s)
如果您使用的是相对较新版本的Boost,我相信线程的ctor有一个方便的重载来处理bind(),允许简单地编写:
boost::thread thrd(boost::bind(&io_service::run, &io));
你能给sau_timer::exec声明吗?它是一个无参数的成员函数,还是一个参数的静态函数