boost :: asio :: async_read绑定编译错误 [英] boost::asio::async_read bind compilation error

查看:831
本文介绍了boost :: asio :: async_read绑定编译错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找不到为什么会收到这个错误:

I can't figure out why I get this error :

/usr/local/include/boost/asio/impl/read.hpp: In member function ‘void boost::asio::detail::read_op<AsyncReadStream, boost::asio::mutable_buffers_1, CompletionCondition, ReadHandler>::operator()(const boost::system::error_code&, size_t, int) [with AsyncReadStream = boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::stream_socket_service<boost::asio::ip::tcp> >, CompletionCondition = boost::asio::detail::transfer_at_least_t, ReadHandler = boost::function<void ()(long unsigned int)>]’:
/usr/local/include/boost/asio/impl/read.hpp:263:   instantiated from ‘void boost::asio::async_read(AsyncReadStream&, const MutableBufferSequence&, CompletionCondition, ReadHandler) [with AsyncReadStream = boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::stream_socket_service<boost::asio::ip::tcp> >, MutableBufferSequence = boost::asio::mutable_buffers_1, CompletionCondition = boost::asio::detail::transfer_at_least_t, ReadHandler = boost::function<void ()(long unsigned int)>]’
src/communicator/protocol/Command.cc:34:   instantiated from here
/usr/local/include/boost/asio/impl/read.hpp:215: error: no match for call to ‘(boost::function<void ()(long unsigned int)>) (const boost::system::error_code&, const long unsigned int&)’
/usr/local/include/boost/function/function_template.hpp:1007: note: candidates are: typename boost::function1<R, T1>::result_type boost::function1<R, T1>::operator()(T0) const [with R = void, T0 = long unsigned int]
make: *** [src/communicator/protocol/Command.o] Error 1

这里我的类:
Command.hh

Here my class : Command.hh

namespace communicator {                                              
  namespace protocol {                                                
    namespace in {                                                    
      class Command : public boost::enable_shared_from_this<Command> {
      public:                                                         
        ~Command();                                                   

        typedef boost::shared_ptr<Command>      pointer;              

        void got_newline();                                           

      protected:                                                      
        Command(tcp::socket& socket, structure::Client& client) :     
          m_socket(socket), m_client(client)  {};

        void endParsing();

        tcp::socket&            m_socket;                             

        structure::Client&      m_client;                             
        char                    m_newline[2];                          
      private:                             

      };                                                              
    }
}   

Command.cc:

Command.cc :

namespace   communicator {
    namespace protocol {
    namespace in {

      void Command::endParsing()  {
        boost::function<void()> cb = boost::bind(&Command::got_newline,
                                          shared_from_this());
        boost::asio::async_read(m_socket,
                                boost::asio::buffer(m_newline, 2),
                                boost::asio::transfer_at_least(2),
**ERROR POINTING THIS LINE**                                    cb);
      }

      void Command::got_newline()  {
        if (m_newline[0] == '\r' && m_newline[1] == '\n') {
           std::cout << "End" << std::endl;
          }
      }

    }
  }
}

检查代码块上的** Error pointing this line **,这是它有一个问题...不知道为什么,一次又一次地破坏了我的头...

Check for the "** Error pointing this line**" on the Code block, this is where it has an issue... not sure why, broke my head again and again...

感谢帮助

为了清晰起见,我删除了一些代码,如果您有任何问题,请不要犹豫

推荐答案

您的完成处理程序签名不正确, b
$ b

Your completion handler signature is not correct, consider this example

#include <boost/asio.hpp>

#include <boost/function.hpp>
#include <boost/bind.hpp>

void
foo()
{

}

int
main()
{
    boost::asio::io_service io_service;
    boost::asio::ip::tcp::socket socket( io_service );

    char buf[2];

    // this compiles file
    boost::asio::async_read(
            socket,
            boost::asio::buffer(buf),
            boost::asio::transfer_at_least(2),
            boost::bind( &foo )
            );

    // this does not
    boost::function<void()> cb = boost::bind( &foo );
    boost::asio::async_read(
            socket,
            boost::asio::buffer(buf),
            boost::asio::transfer_at_least(2),
            cb
            );

}

boost :: bind 足够聪明,不能将错误 bytes_transferred 参数传递给绑定函数指针。 Asio图书馆的作者有关于使用详细博文与库绑定。这是值得读的。

boost::bind is smart enough to not pass the error or bytes_transferred parameters to your bound function pointer. The author of the Asio library has a detailed blog post about using bind with the library. It is worth the read.

这篇关于boost :: asio :: async_read绑定编译错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆