Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boost asio error when building class

I'm trying to feel my way through doing some network programming using the Boost:asio libraries. I've been using an asynchronous handler to transfer data from a given address and it works fine when I leverage asio as functions:

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);
        }
}

Now, I'm trying to integrate these functions into a class but Visual Studio keeps complaining about the way I'm implementing the functions in the class.

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.

Anyone got any pointers on what I'm doing wrong moving these into a class?

I've tried implementing the recommend change in the error message, but it causes errors in the boost libraries themselves.

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>
like image 296
mekelly Avatar asked Aug 15 '26 07:08

mekelly


1 Answers

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

Assuming read_handler is a member function accepting 2 arguments, it should be: m_sock.async_read_some(boost::asio::buffer( m_buffer), boost::bind(&MyClass::read_handler, this, _1, _2));

If you want to use shared_from_this idiom (i.e. your class inherits from enable_shared_from_this), then use shared_from_this() instead of this in bind expression.

like image 171
Igor R. Avatar answered Aug 17 '26 20:08

Igor R.