[英]boost::bind, boost::asio, boost::thread, and classes
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);
}
這是我在類'sau_timer'的構造函數中的代碼(希望在單獨的線程中運行一個計時器,然后調用另一個函數)。
不幸的是,當我嘗試編譯時,我收到以下錯誤:
1> c:\\ program files \\ boost \\ boost_1_39 \\ boost \\ bind \\ bind.hpp(246):錯誤C2064:term不計算為帶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
生成boost :: thread的ctor時發生錯誤。 它需要一個函數對象(帶有opererator()()的東西),你傳遞它(我猜)是一個io :: service。 如果你想要的是一個調用io_service :: run的線程,請寫:
boost::thread thrd(boost::bind(&io_service::run, &io));
如果你使用相對較新版本的Boost,我相信線程的ctor有一個方便的重載來處理bind(),允許簡單地寫:
boost::thread thrd(&io_service::run, &io);
每個非靜態成員函數都有一個第一個隱藏參數 - 要調用函數的實例。 所以你的exec函數需要兩個參數。 你有適當的代碼,但它被注釋掉了。 我的意思是:
t.async_wait(boost::bind(&sau_timer::exec, this, _1));
你嘗試過它還有其他一些問題嗎?
我需要它與strnd.wrap()一起使用。 我又把它改成了這個:
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>------ 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)
==========構建:0成功,1失敗,0最新,0跳過==========
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.