C++:BOOST-bind error: no matching function for call to 'bind(<unresolved overloaded function type>, ...?

StackOverflow https://stackoverflow.com/questions/15697082

  •  30-03-2022
  •  | 
  •  

Question

I tried to turn this boost-asio server into a class and I got this one error trying to compile it,

C:\Documents and Settings\tcpip_server\TCPIP_server.h||In member function 'void TCPIP_server::server(boost::asio::io_service&, short int)':|
C:\Documents and Settings\tcpip_server\TCPIP_server.h|56|error: no matching function for call to 'bind(<unresolved overloaded function type>, boost::shared_ptr<boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::stream_socket_service<boost::asio::ip::tcp> > >&)'|
c:\program files\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\..\..\..\..\include\boost\bind\bind.hpp|1472|note: candidates are: boost::_bi::bind_t<boost::_bi::unspecified, F, typename boost::_bi::list_av_1<A1>::type> boost::bind(F, A1) [with F = void (TCPIP_server::*)(boost::shared_ptr<boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::stream_socket_service<boost::asio::ip::tcp> > >), A1 = boost::shared_ptr<boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::stream_socket_service<boost::asio::ip::tcp> > >]|
c:\program files\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\..\..\..\..\include\boost\bind\bind.hpp|1728|note:                 boost::_bi::bind_t<typename boost::_bi::dm_result<M T::*, A1>::type, boost::_mfi::dm<M, T>, typename boost::_bi::list_av_1<A1>::type> boost::bind(M T::*, A1) [with A1 = boost::shared_ptr<boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::stream_socket_service<boost::asio::ip::tcp> > >, M = void(boost::shared_ptr<boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::stream_socket_service<boost::asio::ip::tcp> > >), T = TCPIP_server]|
||=== Build finished: 1 errors, 3 warnings ===|

I know I'm supposed to somehow cast the type for the session function but there is no ambiguity since only one void function exists. I did check this thread but I'm still confused by the error. Heres the code.

main.cpp

#include "TCPIP_server.h"

int main()
{

    return 0;
}

TCPIP_server.h

#include <boost/thread/thread.hpp>
#include <boost/bind.hpp>
#include <boost/smart_ptr.hpp>
#include <boost/asio.hpp>
#include <cstdlib>
#include <iostream>


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

const int max_length = 1024;

class TCPIP_server
{
        private:

            typedef boost::shared_ptr<tcp::socket> socket_ptr;
            char data[max_length];

        public:

        TCPIP_server(){}
        ~TCPIP_server(){}
        void session(socket_ptr sock)
        {
            try
            {
                for (;;)
                {
                    char data[max_length];

                    boost::system::error_code error;
                    size_t length = sock->read_some(boost::asio::buffer(data), error);
                    if (error == boost::asio::error::eof)
                        break; // Connection closed cleanly by peer.
                    else if (error)
                        throw boost::system::system_error(error); // Some other error.

                    boost::asio::write(*sock, boost::asio::buffer(data, length));
                }
            }
            catch (std::exception& e)
            {
                std::cerr << "Exception in thread: " << e.what() << "\n";
            }
        }
        void server(boost::asio::io_service& io_service, short port)
        {
            tcp::acceptor a(io_service, tcp::endpoint(tcp::v4(), port));

            for (;;)
            {
                socket_ptr sock(new tcp::socket(io_service));
                a.accept(*sock);
                boost::thread t(boost::bind(session, sock));
            }
        }
        void main_server()
        {
            try
            {
                std::cerr << "Usage: blocking_tcp_echo_server \n";
                boost::asio::io_service io_service;

                using namespace std; // For atoi.
                server(io_service, atoi("4001"));
            }
            catch (std::exception& e)
            {
                std::cerr << "Exception: " << e.what() << "\n";
            }
        }
};
Was it helpful?

Solution

The bind needs the TCPIP_server's class instance on which to call the session method. So your code should be:

boost::bind(&TCPIP_server::session, this, sock)

The TCPIP_server:: is mandatory to be fully C++03 compatible on all platforms/compilers.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top