C ++:BOOST绑定错误:没有匹配的函数调用'bind(<未解析的重载函数类型>,...? [英] C++:BOOST-bind error: no matching function for call to 'bind(<unresolved overloaded function type>, ...?

查看:3203
本文介绍了C ++:BOOST绑定错误:没有匹配的函数调用'bind(<未解析的重载函数类型>,...?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试这种提升-asio server into a class,我得到这个错误尝试编译它

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

我知道我应该以某种方式为 session 函数,但没有歧义,因为只有一个 void 函数存在。我没有检查此主题,但我还是困惑的错误。代码

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

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


推荐答案

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

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)

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

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

这篇关于C ++:BOOST绑定错误:没有匹配的函数调用'bind(&lt;未解析的重载函数类型&gt;,...?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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