C ++:BOOST-bind错误:没有用于调用&#39; bind的匹配函数(<unresolved overloaded =“”function =“”type =“”>,...?</unresolved>

时间:2013-03-29 03:56:16

标签: c++ boost-asio boost-bind

我试图将this boost-asio server变成一个类,我在尝试编译它时遇到了一个错误,

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 ===|

我知道我应该以某种方式为session函数转换类型,但没有歧义,因为只存在一个void函数。我确实检查了this thread,但我仍然对错误感到困惑。这是代码。

的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";
            }
        }
};

1 个答案:

答案 0 :(得分:3)

bind需要调用TCPIP_server方法的session类实例。所以你的代码应该是:

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

TCPIP_server::必须在所有平台/编译器上完全兼容C ++ 03。