3

我试图通过使用 Boost:asio 库进行一些网络编程来感受我的方式。我一直在使用异步处理程序从给定地址传输数据,当我利用 asio 作为函数时它工作正常:

boost::asio::io_service io_service;
boost::asio::ip::tcp::resolver resolver( io_service);
boost::asio::ip::tcp::socket sock( io_service);
boost::array < char, 4096 > buffer;

void read_handler( const boost:: system:: error_code &ec,
        std:: size_t bytes_transferred)
{
    if (! ec)
        {
            if (std::string(buffer.data(), bytes_transferred) == "$")
            {
                    myss.str(std::string());
            }
            myss << std::string(buffer.data(), bytes_transferred);
            if (int(buffer.data()[0]) == 10)
                {
                    ....

                }
            std::cout << std::string(buffer.data(), bytes_transferred) ;//<< std::endl;
            sock.async_read_some( boost::asio::buffer( buffer), read_handler);
        }
}

void connect_handler( const boost::system::error_code &ec)
{
    if (! ec)
        {
            boost::asio::write( sock, boost::asio::buffer(
                    "GET / HTTP 1.1\r\nHost: www.google.com\r\n\r\n"));
            sock.async_read_some( boost::asio::buffer( buffer), read_handler);
        }
}

void resolve_handler( const boost::system::error_code &ec,
        boost:: asio::ip::tcp::resolver::iterator it)
{
    if (! ec)
        {
            sock.async_connect(* it, connect_handler);
        }
}

现在,我试图将这些函数集成到一个类中,但 Visual Studio 一直抱怨我在类中实现这些函数的方式。

void
MyClass::connect_handler( const boost::system::error_code &ec)
{
    if (! ec)
        {
            //boost::asio::write( sock, boost::asio::buffer(
            //        "GET / HTTP 1.1\r\nHost: www.highscore.de\r\n\r\n"));
            m_sock.async_read_some( boost::asio::buffer( m_buffer), read_handler);
        }
}

    : error C3867: 'MyClass::read_handler': function call missing argument list; use '&MyClass::read_handler' to create a pointer to member: 
    error C3867: 'MyClass::read_handler': function call missing argument list; use '&MyClass::read_handler' to create a pointer to member
    2>'MyClass::connect_handler': function call missing argument list; use 

'&MyClass::connect_handler' to create a pointer to member
2>
2>Build FAILED.

任何人都对我在将这些移入类中做错了什么有任何指示?

我已经尝试在错误消息中实施建议的更改,但它会导致 boost 库本身出现错误。

2>C:\installs\Boost\include\boost-1_49\boost/asio/basic_stream_socket.hpp(785): error C2338: ReadHandler type requirements not met
2>          : see reference to function template instantiation 'void boost::asio::basic_stream_socket<Protocol>::async_read_some<boost::asio::mutable_buffers_1,void(__thiscall MyClass::Sensor::* )(const boost::system::error_code &,size_t)>(const MutableBufferSequence &,const ReadHandler &)' being compiled
2>          with
2>          [
2>              Protocol=boost::asio::ip::tcp,
2>              MutableBufferSequence=boost::asio::mutable_buffers_1,
2>              ReadHandler=void (__thiscall MyClass::Sensor::* )(const boost::system::error_code &,size_t)
2>          ]
2>C:\installs\Boost\include\boost-1_49\boost/asio/basic_stream_socket.hpp(785): error C2064: term does not evaluate to a function taking 2 arguments
2>C:\installs\Boost\include\boost-1_49\boost/asio/basic_socket.hpp(706): error C2338: ConnectHandler type requirements not met
2>           : see reference to function template instantiation 'void boost::asio::basic_socket<Protocol,SocketService>::async_connect<void(__thiscall MyClass::Sensor::* )(const boost::system::error_code &)>(const boost::asio::ip::basic_endpoint<InternetProtocol> &,const ConnectHandler &)' being compiled
2>          with
2>          [
2>              Protocol=boost::asio::ip::tcp,
2>              SocketService=boost::asio::stream_socket_service<boost::asio::ip::tcp>,
2>              InternetProtocol=boost::asio::ip::tcp,
2>              ConnectHandler=void (__thiscall MyClass::Sensor::* )(const boost::system::error_code &)
2>          ]
2>C:\installs\Boost\include\boost-1_49\boost/asio/basic_socket.hpp(706): error C2064: term does not evaluate to a function taking 1 arguments
2>
4

2 回答 2

5

m_sock.async_read_some(boost::asio::buffer(m_buffer), read_handler);

假设read_handler是一个接受 2 个参数的成员函数,它应该是:m_sock.async_read_some(boost::asio::buffer( m_buffer), boost::bind(&MyClass::read_handler, this, _1, _2));

如果你想使用shared_from_this成语(即你的类继承自enable_shared_from_this),那么使用shared_from_this()代替thisinbind表达式。

于 2012-10-17T21:26:06.687 回答
1

您不能仅通过命名成员来创建指向成员的指针,您需要使用类限定显式请求它:

&MyClass::connect_handler

(正如错误消息非常清楚地指出......)

于 2012-10-17T17:49:08.857 回答