0

现在我想在前一个问题的函数中添加两个参数:boost::bind 和 boost::asio。boost::bind 不起作用,从示例中复制

编辑:抱歉,忘记添加 timer_result 的类型: boost::optional timer_result;

//my function
void CClient::setResult2(boost::optional<boost::system::error_code>* a, boost::system::error_code b,
const boost::system::error_code& error, std::size_t);

//my attempt: I'd like to set the value of the first, third and fourth parameters but not the second one
timer.async_wait(boost::bind(&CClient::setResult2, this, &timer_result, _1,boost::asio::placeholders::error,
              boost::asio::placeholders::bytes_transferred));

_1 的含义是否与上一个问题不同?谢谢。

错误:

Description     Resource        Path    Location        Type  
required from ‘void boost::asio::basic_deadline_timer<Time,
TimeTraits, TimerService>::async_wait(WaitHandler&&) [with WaitHandler
= boost::_bi::bind_t<void, boost::_mfi::mf4<void, CClient, boost::optional<boost::system::error_code>*,
boost::system::error_code, const boost::system::error_code&, unsigned
int>, boost::_bi::list5<boost::_bi::value<CClient*>,
boost::_bi::value<boost::optional<boost::system::error_code>*>,
boost::arg<1>, boost::arg<1> (*)(), boost::arg<2> (*)()> >; Time =
boost::posix_time::ptime; TimeTraits =
boost::asio::time_traits<boost::posix_time::ptime>; TimerService =
boost::asio::deadline_timer_service<boost::posix_time::ptime,
boost::asio::time_traits<boost::posix_time::ptime> >]’   remote_server
line 501, external location:
/usr/include/boost/asio/basic_deadline_timer.hpp   C/C++ Problem
invalid user-defined conversion from ‘const boost::system::error_code’
to ‘unsigned int’ [-fpermissive] remote_server           line 525,
external location: /usr/include/boost/bind/bind.hpp   C/C++ Problem  
required from ‘void boost::_bi::list5<A1, A2, A3, A4,
A5>::operator()(boost::_bi::type<void>, F&, A&, int) [with F =
boost::_mfi::mf4<void, CClient,
boost::optional<boost::system::error_code>*,
boost::system::error_code, const boost::system::error_code&, unsigned
int>; A = boost::_bi::list1<const boost::system::error_code&>; A1 =
boost::_bi::value<CClient*>; A2 =
boost::_bi::value<boost::optional<boost::system::error_code>*>; A3 =
boost::arg<1>; A4 = boost::arg<1> (*)(); A5 = boost::arg<2> (*)()]’   
remote_server           line 525, external location:
/usr/include/boost/bind/bind.hpp   C/C++ Problem size of array is
negative       remote_server           line 37, external location:
/usr/include/boost/bind/arg.hpp     C/C++ Problem make: ***
[src/CClient.o] Error 1       remote_server                   C/C++
Problem   required from ‘void boost::asio::detail::binder1<Handler,
Arg1>::operator()() [with Handler = boost::_bi::bind_t<void,
boost::_mfi::mf4<void, CClient,
boost::optional<boost::system::error_code>*,
boost::system::error_code, const boost::system::error_code&, unsigned
int>, boost::_bi::list5<boost::_bi::value<CClient*>,
boost::_bi::value<boost::optional<boost::system::error_code>*>,
boost::arg<1>, boost::arg<1> (*)(), boost::arg<2> (*)()> >; Arg1 =
boost::system::error_code]’     remote_server           line 46,
external location: /usr/include/boost/asio/detail/bind_handler.hpp    
C/C++ Problem   required from ‘boost::_bi::bind_t<R, F,
L>::result_type boost::_bi::bind_t<R, F, L>::operator()(const A1&)
[with A1 = boost::system::error_code; R = void; F =
boost::_mfi::mf4<void, CClient,
boost::optional<boost::system::error_code>*,
boost::system::error_code, const boost::system::error_code&, unsigned
int>; L = boost::_bi::list5<boost::_bi::value<CClient*>,
boost::_bi::value<boost::optional<boost::system::error_code>*>,
boost::arg<1>, boost::arg<1> (*)(), boost::arg<2> (*)()>;
boost::_bi::bind_t<R, F, L>::result_type = void]’    remote_server    
line 47, external location: /usr/include/boost/bind/bind_template.hpp 
C/C++ Problem   required from ‘void
boost_asio_handler_invoke_helpers::invoke(Function&, Context&) [with
Function = boost::asio::detail::binder1<boost::_bi::bind_t<void,
boost::_mfi::mf4<void, CClient,
boost::optional<boost::system::error_code>*,
boost::system::error_code, const boost::system::error_code&, unsigned
int>, boost::_bi::list5<boost::_bi::value<CClient*>,
boost::_bi::value<boost::optional<boost::system::error_code>*>,
boost::arg<1>, boost::arg<1> (*)(), boost::arg<2> (*)()> >,
boost::system::error_code>; Context = boost::_bi::bind_t<void,
boost::_mfi::mf4<void, CClient,
boost::optional<boost::system::error_code>*,
boost::system::error_code, const boost::system::error_code&, unsigned
int>, boost::_bi::list5<boost::_bi::value<CClient*>,
boost::_bi::value<boost::optional<boost::system::error_code>*>,
boost::arg<1>, boost::arg<1> (*)(), boost::arg<2> (*)()> >]’
remote_server           line 39, external location:
/usr/include/boost/asio/detail/handler_invoke_helpers.hpp   C/C++
Problem   required from ‘void
boost::asio::asio_handler_invoke(Function, ...) [with Function =
boost::asio::detail::binder1<boost::_bi::bind_t<void,
boost::_mfi::mf4<void, CClient,
boost::optional<boost::system::error_code>*,
boost::system::error_code, const boost::system::error_code&, unsigned
int>, boost::_bi::list5<boost::_bi::value<CClient*>,
boost::_bi::value<boost::optional<boost::system::error_code>*>,
boost::arg<1>, boost::arg<1> (*)(), boost::arg<2> (*)()> >,
boost::system::error_code>]’       remote_server           line 64,
external location: /usr/include/boost/asio/handler_invoke_hook.hpp    
C/C++ Problem   required from
‘boost::asio::detail::wait_handler<Handler>::wait_handler(Handler&)
[with Handler = boost::_bi::bind_t<void, boost::_mfi::mf4<void,
CClient, boost::optional<boost::system::error_code>*,
boost::system::error_code, const boost::system::error_code&, unsigned
int>, boost::_bi::list5<boost::_bi::value<CClient*>,
boost::_bi::value<boost::optional<boost::system::error_code>*>,
boost::arg<1>, boost::arg<1> (*)(), boost::arg<2> (*)()> >]’  
remote_server           line 39, external location:
/usr/include/boost/asio/detail/wait_handler.hpp     C/C++ Problem  
required from ‘static void
boost::asio::detail::wait_handler<Handler>::do_complete(boost::asio::detail::io_service_impl*,
boost::asio::detail::operation*, const boost::system::error_code&,
std::size_t) [with Handler = boost::_bi::bind_t<void,
boost::_mfi::mf4<void, CClient,
boost::optional<boost::system::error_code>*,
boost::system::error_code, const boost::system::error_code&, unsigned
int>, boost::_bi::list5<boost::_bi::value<CClient*>,
boost::_bi::value<boost::optional<boost::system::error_code>*>,
boost::arg<1>, boost::arg<1> (*)(), boost::arg<2> (*)()> >;
boost::asio::detail::io_service_impl =
boost::asio::detail::task_io_service; boost::asio::detail::operation =
boost::asio::detail::task_io_service_operation; std::size_t = unsigned
int]’ remote_server           line 69, external location:
/usr/include/boost/asio/detail/wait_handler.hpp     C/C++ Problem
make: *** No rule to make target `all'.                         C/C++
Problem   required from ‘void
boost::asio::deadline_timer_service<TimeType,
TimeTraits>::async_wait(boost::asio::deadline_timer_service<TimeType,
TimeTraits>::implementation_type&, WaitHandler&&) [with WaitHandler =
boost::_bi::bind_t<void, boost::_mfi::mf4<void, CClient,
boost::optional<boost::system::error_code>*,
boost::system::error_code, const boost::system::error_code&, unsigned
int>, boost::_bi::list5<boost::_bi::value<CClient*>,
boost::_bi::value<boost::optional<boost::system::error_code>*>,
boost::arg<1>, boost::arg<1> (*)(), boost::arg<2> (*)()> >; TimeType =
boost::posix_time::ptime; TimeTraits =
boost::asio::time_traits<boost::posix_time::ptime>;
boost::asio::deadline_timer_service<TimeType,
TimeTraits>::implementation_type =
boost::asio::detail::deadline_timer_service<boost::asio::time_traits<boost::posix_time::ptime>
>::implementation_type]’ remote_server           line 138, external location: /usr/include/boost/asio/deadline_timer_service.hpp C/C++
Problem   required from ‘void
boost::asio::detail::deadline_timer_service<Time_Traits>::async_wait(boost::asio::detail::deadline_timer_service<Time_Traits>::implementation_type&,
Handler) [with Handler = boost::_bi::bind_t<void,
boost::_mfi::mf4<void, CClient,
boost::optional<boost::system::error_code>*,
boost::system::error_code, const boost::system::error_code&, unsigned
int>, boost::_bi::list5<boost::_bi::value<CClient*>,
boost::_bi::value<boost::optional<boost::system::error_code>*>,
boost::arg<1>, boost::arg<1> (*)(), boost::arg<2> (*)()> >;
Time_Traits = boost::asio::time_traits<boost::posix_time::ptime>]’
remote_server           line 180, external location:
/usr/include/boost/asio/detail/deadline_timer_service.hpp  C/C++
Problem   required from here    CClient.cpp     /remote_server/src    
line 102        C/C++ Problem

信息:

Description     Resource        Path    Location        Type candidate is: 
boost::system::error_code::operator boost::system::error_code::unspecified_bool_type() const <near match>
remote_server           line 355, external location:
/usr/include/boost/system/error_code.hpp   C/C++ Problem   no known conversion for implicit ‘this’ parameter from ‘boost::system::error_code::unspecified_bool_type {aka void (*)()}’ to ‘unsigned int’      remote_server           line 355, external
location: /usr/include/boost/system/error_code.hpp   C/C++ Problem
4

3 回答 3

0

async_wait()接受具有此签名的函数(来自 Boost.ASIO 文档):

void handler(
  const boost::system::error_code& error // Result of operation.
);

您的绑定会导致功能类似的事情,其签名类似于:

void bound_setResult2(_1, _error, _bytes_transferred);

bind() 调用中的每个占位符都映射到绑定结果中的一个参数。你有三个占位符。当然编译不了。但是你有一个更大的概念问题。

您似乎认为截止时间计时器知道正在进行的 I/O。它不是。处理程序仅在计时器经过或取消时才被调用。(并且错误代码表明它是否被取消。)不应期望计时器处理程序知道#bytes 已传输。由您的 I/O 处理程序(而不是计时器过期处理程序)来跟踪该信息。

可能,您真正想要的计时器处理程序中的所有代码都是调用的代码, port.cancel()然后setResults2()——它应该是 I/O 的处理程序,而不是计时器的处理程序——你处理取消。

于 2012-09-25T19:17:10.110 回答
0

您的方法中有两个错误代码,bind 中只有一个错误占位符。

于 2012-09-11T10:16:55.583 回答
0

placeholders::error_1是,placeholders::bytes_transferred实际上是 的别名_2,所以你不能再放_1了。

将您的附加参数移动到第三个:并作为第三个参数void CClient::setResult2(const boost::system::error_code& error, std::size_t, boost::optional<boost::system::error_code>* a)传递。&timer_result像这样:

bind(&CClient::setResult2, this, _1, _2, &timer_result).

(我在这里使用了较短的占位符,但如果您喜欢,可以用较长的 asio 替换它们。)

于 2012-09-11T12:29:14.897 回答