1

我试图用 std::bind 绑定一个函数,它一直说我的参数集没有重载。它与 boost::bind 一起工作。std 和 boost 绑定有什么区别?

我在用着:

Microsoft Visual Studio Ultimate 2012 版本 11.0.60315.01 更新 2

提升 1.53.0

func 声明和绑定:

void Messenger::receiveCallback(const boost::system::error_code& error, size_t bytes_transferred, char* message, int bufferSize, tcp::socket* socketPtr, void(*onMessageReceivedCallback)(char* message, string hostname, int port, int length));

std::bind(&Messenger::receiveCallback, this, std::placeholders::_1, std::placeholders::_2, message, bufferSize, socketPtr, onMessageReceivedCallback);//error

boost::bind(&Messenger::receiveCallback, this, _1, _2, message, bufferSize, socketPtr, onMessageReceivedCallback);

错误:

105 IntelliSense:没有重载函数“std::bind”的实例与参数列表参数类型匹配:(void (Messenger::*)(const boost::system::error_code &error, size_t bytes_transferred, char *message, int bufferSize , boost::asio::ip::tcp::socket *socketPtr, void (*onMessageReceivedCallback)(char *message, std::string hostname, int port, int length)), Messenger *, std::_Ph<1 >, std::_Ph<2>, char *, int, boost::asio::ip::tcp::socket , void ( )(char *message, std::string hostname, int port, int length)) c:\Users\gospo_000\Source\Repos\Messenger\Messenger\Messenger\Messenger.cpp 176 4 Messenger

很多像这样的错误:

错误 81 错误 C2780:'enable_if::value,std::_BindRx(_thiscall _Farg0::*)(_V0_t,_V1_t,_V2_t,_V3_t,_V4_t),_Rx,_Farg0,_V0_t,_V1_t,_V2_t,_V3_t,_V4_t>,_Vx0_t ,_Vx1_t>>::type std::bind( Rx ( _thiscall _Farg0::* const )(_V0_t,_V1_t,_V2_t,_V3_t,_V4_t),_Vx0_t &&,_Vx1_t &&)' :需要 3 个参数 - 提供 8 个 c:\ users\gospo_000\source\repos\messenger\messenger\messenger\messenger.cpp 176 1 tester2

信使.h

#include <string>
#include <boost/asio.hpp>
#include <boost/thread.hpp>
#include "Connection.h"
#include "Message.h"
#include <unordered_map>

using namespace std;
using namespace boost::asio;
using boost::asio::ip::tcp;


class Messenger
    {
   //.............................
          void receiveCallback(const boost::system::error_code& error, size_t bytes_transferred, char* message, int bufferSize, tcp::socket* socketPtr, void(*onMessageReceivedCallback)(char* message, string hostname, int port, int length));
    };

信使.cpp

void Messenger::receiveCallback(const boost::system::error_code& error, size_t bytes_transferred, char* message, int bufferSize, tcp::socket* socketPtr, void(*onMessageReceivedCallback)(char* message, string hostname, int port, int length))
{
    if(error)
    {
        string connectionId = getConnectionId(socketPtr->remote_endpoint());
        connections_.erase(connectionId);
        cout<<error.message();
    }
    else
    {
        onMessageReceivedCallback(message, socketPtr->remote_endpoint().address().to_string(), socketPtr->remote_endpoint().port(), bytes_transferred);
        socketPtr->async_receive(
            boost::asio::buffer(message, bufferSize),
            boost::bind(&Messenger::receiveCallback, this, _1, _2, message, bufferSize, socketPtr, onMessageReceivedCallback));
    }
}
4

1 回答 1

1

不同之处在于默认情况下std::bind只接受5 个参数。将 _VARIADIC_MAX 宏设置为 10,以增加可变参数模板中的最大 arg 数量。

于 2013-05-23T10:20:07.053 回答