使用boost :: async_connect问题

时间:2014-06-16 13:41:58

标签: c++ boost boost-asio

我没有问题摆弄asio的阻塞套接字,但是我对异步套件的深入研究一直是个噩梦。也许我只是筋疲力尽:

  using boost::asio::ip::tcp;

  boost::asio::io_service io_service;

  tcp::resolver resolver(io_service);
  tcp::resolver::query query(newip, newport);
  tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);

  tcp::socket socket(io_service);
  boost::system::error_code ec;

  auto f = std::bind(&Irc::connect_handler, this, boost::asio::placeholders::error,
    boost::asio::placeholders::iterator);
  boost::asio::async_connect(socket, endpoint_iterator, f);

我的处理程序被定义为Irc类中的成员函数:

  void connect_handler(
    const boost::system::error_code& ec,
    boost::asio::ip::tcp::resolver::iterator i)
  {
    // ...
  }

我在VS2013上,错误不是很好

1>------ Build started: Project: Project18, Configuration: Debug Win32 ------
1>  Irc.cpp
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\functional(1149): error C2664: 'void std::_Pmf_wrap<void (__thiscall Irc::* )(const boost::system::error_code &,boost::asio::ip::basic_resolver_iterator<InternetProtocol>),void,Irc,const boost::system::error_code &,boost::asio::ip::basic_resolver_iterator<InternetProtocol>>::operator ()(_Farg0 &,const boost::system::error_code &,boost::asio::ip::basic_resolver_iterator<InternetProtocol>) const' : cannot convert argument 2 from 'boost::arg<1>' to 'const boost::system::error_code &'
1>          with
1>          [
1>              InternetProtocol=boost::asio::ip::tcp
1>  ,            _Farg0=Irc
1>          ]
1>          Reason: cannot convert from 'boost::arg<1>' to 'const boost::system::error_code'
1>          No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\functional(1137) : see reference to function template instantiation 'void std::_Bind<true,void,std::_Pmf_wrap<void (__thiscall Irc::* )(const boost::system::error_code &,boost::asio::ip::basic_resolver_iterator<InternetProtocol>),void,Irc,const boost::system::error_code &,boost::asio::ip::basic_resolver_iterator<InternetProtocol>>,Irc *const ,boost::arg<1> &,boost::arg<2> &>::_Do_call<Arg1,Arg2,0,1,2>(std::tuple<Arg1 &,Arg2 &>,std::_Arg_idx<0,1,2>)' being compiled
1>          with
1>          [
1>              InternetProtocol=boost::asio::ip::tcp
1>  ,            Arg1=const boost::system::error_code
1>  ,            Arg2=boost::asio::ip::basic_resolver_iterator<boost::asio::ip::tcp>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\functional(1137) : see reference to function template instantiation 'void std::_Bind<true,void,std::_Pmf_wrap<void (__thiscall Irc::* )(const boost::system::error_code &,boost::asio::ip::basic_resolver_iterator<InternetProtocol>),void,Irc,const boost::system::error_code &,boost::asio::ip::basic_resolver_iterator<InternetProtocol>>,Irc *const ,boost::arg<1> &,boost::arg<2> &>::_Do_call<Arg1,Arg2,0,1,2>(std::tuple<Arg1 &,Arg2 &>,std::_Arg_idx<0,1,2>)' being compiled
1>          with
1>          [
1>              InternetProtocol=boost::asio::ip::tcp
1>  ,            Arg1=const boost::system::error_code
1>  ,            Arg2=boost::asio::ip::basic_resolver_iterator<boost::asio::ip::tcp>
1>          ]
1>          c:\local\boost_1_55_0\boost\asio\detail\handler_type_requirements.hpp(98) : see reference to function template instantiation 'void std::_Bind<true,void,std::_Pmf_wrap<void (__thiscall Irc::* )(const boost::system::error_code &,boost::asio::ip::basic_resolver_iterator<InternetProtocol>),void,Irc,const boost::system::error_code &,boost::asio::ip::basic_resolver_iterator<InternetProtocol>>,Irc *const ,boost::arg<1> &,boost::arg<2> &>::operator ()<Arg1&,Arg2&>(Arg1 &,Arg2 &)' being compiled
1>          with
1>          [
1>              InternetProtocol=boost::asio::ip::tcp
1>  ,            Arg1=const boost::system::error_code
1>  ,            Arg2=boost::asio::ip::basic_resolver_iterator<boost::asio::ip::tcp>
1>          ]
1>          c:\local\boost_1_55_0\boost\asio\impl\connect.hpp(257) : see reference to function template instantiation 'void std::_Bind<true,void,std::_Pmf_wrap<void (__thiscall Irc::* )(const boost::system::error_code &,boost::asio::ip::basic_resolver_iterator<InternetProtocol>),void,Irc,const boost::system::error_code &,boost::asio::ip::basic_resolver_iterator<InternetProtocol>>,Irc *const ,boost::arg<1> &,boost::arg<2> &>::operator ()<Arg1&,Arg2&>(Arg1 &,Arg2 &)' being compiled
1>          with
1>          [
1>              InternetProtocol=boost::asio::ip::tcp
1>  ,            Arg1=const boost::system::error_code
1>  ,            Arg2=boost::asio::ip::basic_resolver_iterator<boost::asio::ip::tcp>
1>          ]
1>          c:\users\c\documents\visual studio 2012\projects\project18\project18\irc.cpp(37) : see reference to function template instantiation 'boost::asio::async_result<Handler>::type boost::asio::async_connect<Protocol,StreamSocketService,boost::asio::ip::basic_resolver_iterator<InternetProtocol>,std::_Bind<true,void,std::_Pmf_wrap<void (__thiscall Irc::* )(const boost::system::error_code &,boost::asio::ip::basic_resolver_iterator<InternetProtocol>),void,Irc,const boost::system::error_code &,boost::asio::ip::basic_resolver_iterator<InternetProtocol>>,Irc *const ,boost::arg<1> &,boost::arg<2> &>&>(boost::asio::basic_socket<Protocol,StreamSocketService> &,Iterator,ComposedConnectHandler)' being compiled
1>          with
1>          [
1>              Handler=std::_Bind<true,void,std::_Pmf_wrap<void (__thiscall Irc::* )(const boost::system::error_code &,boost::asio::ip::basic_resolver_iterator<boost::asio::ip::tcp>),void,Irc,const boost::system::error_code &,boost::asio::ip::basic_resolver_iterator<boost::asio::ip::tcp>>,Irc *const ,boost::arg<1> &,boost::arg<2> &>
1>  ,            Protocol=boost::asio::ip::tcp
1>  ,            StreamSocketService=boost::asio::stream_socket_service<boost::asio::ip::tcp>
1>  ,            InternetProtocol=boost::asio::ip::tcp
1>  ,            Iterator=boost::asio::ip::basic_resolver_iterator<boost::asio::ip::tcp>
1>  ,            ComposedConnectHandler=std::_Bind<true,void,std::_Pmf_wrap<void (__thiscall Irc::* )(const boost::system::error_code &,boost::asio::ip::basic_resolver_iterator<boost::asio::ip::tcp>),void,Irc,const boost::system::error_code &,boost::asio::ip::basic_resolver_iterator<boost::asio::ip::tcp>>,Irc *const ,boost::arg<1> &,boost::arg<2> &> &
1>          ]
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

2 个答案:

答案 0 :(得分:3)

如果您使用std::bind,则还需要使用std::placeholders。 asio占位符仅适用于boost::bind

所以这应该解决它:

auto f = std::bind(&Irc::connect_handler, this, std::placeholders::_1, std::placeholders::_2);

答案 1 :(得分:0)

您需要将boost::bind与asio占位符一起使用