c ++ - boost ASIO编译器错误未找到匹配函数

时间:2017-01-07 10:31:59

标签: c++ boost network-programming

我创建了一个Server类和一个Session类。以下是我的Server类的函数:

void Server::startAccept(){
    std::shared_ptr<Session> newConnection =
                       Session::create(m_acceptor.get_io_service());

    //m_acceptor is a member variable of the `Server` class
    m_acceptor.async_accept(newConnection->getSocket(),
            std::bind(&Server::acceptHandler, this, newConnection, 
                                                   placeholders::error));
}

以及接受连接时的处理函数:

void Server::acceptHandler(std::shared_ptr<Session> newConnection,  const boost::system::error_code& error){
    if (!error)
        {
          std::cout << "A client connected" << std::endl;
          newConnection->start(); // start() is a function of Session Class
        }
    startAccept();
}

但是在编译时我收到错误:

../src/Server.cpp: In member function ‘void Server::startAccept()’:
../src/Server.cpp:26:85: error: no matching function for call to ‘boost::asio::basic_socket_acceptor<boost::asio::ip::tcp>::async_accept(boost::asio::ip::tcp&, std::_Bind_helper<false, void (Server::*)(std::shared_ptr<Session>, const boost::system::error_code&), Server*, std::shared_ptr<Session>&, boost::arg<1> (&)()>::type)’
          std::bind(&Server::acceptHandler, this, newConnection, placeholders::error));
                                                                                     ^
In file included from /usr/include/boost/asio.hpp:28:0,
                 from ../src/Server.h:16,
                 from ../src/Server.cpp:8:
/usr/include/boost/asio/basic_socket_acceptor.hpp:1009:3: note: candidate: template<class Protocol1, class SocketService, class AcceptHandler> typename boost::asio::async_result<typename boost::asio::handler_type<AcceptHandler, void(boost::system::error_code)>::type>::type boost::asio::basic_socket_acceptor<Protocol, SocketAcceptorService>::async_accept(boost::asio::basic_socket<Protocol1, SocketService1>&, AcceptHandler&&, typename std::enable_if<std::is_convertible<Protocol, Protocol1>::value>::type*) [with Protocol1 = Protocol1; SocketService = SocketService; AcceptHandler = AcceptHandler; Protocol = boost::asio::ip::tcp; SocketAcceptorService = boost::asio::socket_acceptor_service<boost::asio::ip::tcp>]
   async_accept(basic_socket<Protocol1, SocketService>& peer,
   ^
/usr/include/boost/asio/basic_socket_acceptor.hpp:1009:3: note:   template argument deduction/substitution failed:
../src/Server.cpp:26:85: note:   ‘boost::asio::ip::tcp’ is not derived from ‘boost::asio::basic_socket<Protocol, DatagramSocketService>’
          std::bind(&Server::acceptHandler, this, newConnection, placeholders::error));
                                                                                     ^
In file included from /usr/include/boost/asio.hpp:28:0,
                 from ../src/Server.h:16,
                 from ../src/Server.cpp:8:
/usr/include/boost/asio/basic_socket_acceptor.hpp:1121:3: note: candidate: template<class SocketService, class AcceptHandler> typename boost::asio::async_result<typename boost::asio::handler_type<AcceptHandler, void(boost::system::error_code)>::type>::type boost::asio::basic_socket_acceptor<Protocol, SocketAcceptorService>::async_accept(boost::asio::basic_socket<Protocol, SocketService>&, boost::asio::basic_socket_acceptor<Protocol, SocketAcceptorService>::endpoint_type&, AcceptHandler&&) [with SocketService = SocketService; AcceptHandler = AcceptHandler; Protocol = boost::asio::ip::tcp; SocketAcceptorService = boost::asio::socket_acceptor_service<boost::asio::ip::tcp>]
   async_accept(basic_socket<protocol_type, SocketService>& peer,
   ^
/usr/include/boost/asio/basic_socket_acceptor.hpp:1121:3: note:   template argument deduction/substitution failed:
../src/Server.cpp:26:85: note:   ‘boost::asio::ip::tcp’ is not derived from ‘boost::asio::basic_socket<boost::asio::ip::tcp, SocketService>’
          std::bind(&Server::acceptHandler, this, newConnection, placeholders::error));

有人可以告诉我这里缺少什么吗?

我正在添加所有文件以供参考:

Server.h

#ifndef SERVER_H_
#define SERVER_H_

#include <iostream>
#include <string>
#include<functional>
#include<memory>
#include <boost/asio.hpp>
#include "Session.h"

using namespace boost::asio;

class Server {
  ip::tcp::acceptor m_acceptor;
  io_service m_ioService;


public:
    Server(int port);
    void startAccept(); //calls acceptHandler() on accepting the connection from client
    void acceptHandler(std::shared_ptr<Session> newConnection,  const boost::system::error_code& error);
    virtual ~Server();
};

#endif /* SERVER_H_ */

Server.cpp

#include "Server.h"

Server::Server(int port): m_acceptor(m_ioService, ip::tcp::endpoint(ip::tcp::v4(), port)){

    startAccept();
}

Server::~Server() {    }

void Server::startAccept(){

    //std::shared_ptr<session> newConnection = std::make_shared<session> (session(m_acceptor.get_io_service()));

    std::shared_ptr<Session> newConnection = Session::create(m_acceptor.get_io_service());

    m_acceptor.async_accept(newConnection->getSocket(),
            std::bind(&Server::acceptHandler, this, newConnection, placeholders::error));
}

void Server::acceptHandler(std::shared_ptr<Session> newConnection,  const boost::system::error_code& error){
    if (!error)
        {
          std::cout << "A client connected" << std::endl;
          newConnection->start(); //starts this function when a new connection is accepted
        }
    startAccept();
}

Session.h

#ifndef SESSION_H_
#define SESSION_H_

#include <iostream>
#include <string>
#include<functional>
#include<memory>

#include <boost/asio.hpp>
using namespace boost::asio;

class Session : public std::enable_shared_from_this<Session>{

    ip::tcp::socket m_socket;
    std::string m_acceptMessage;
    std::string m_buffer;
public:
    Session(io_service &ioService);
    virtual ~Session();
    static std::shared_ptr<Session> create(io_service& ioService);
    void start(); //this function is called from server acceptHandler when a connection is accepted by server
    void readHandler(const boost::system::error_code& error,  size_t bytesTransferred); //called in async_read()
    void writeHandler(const boost::system::error_code& error,  size_t bytesTransferred); //called in async_write()
    ip::tcp& getSocket();
    void receiveMessage();
    void sendMessage(const std::string& message);
};

#endif /* SESSION_H_ */

Session.cpp

#include "Session.h"

Session::Session(io_service& ioService): m_socket(ioService) {    }

Session::~Session() {    }

std::shared_ptr<Session> Session::create(io_service& ioService){
    return std::shared_ptr<Session>(new Session(ioService));
}

void Session::start(){
    //read from client connection
    receiveMessage();

    //write to client connection
    sendMessage("dummy message"); 
}

void Session::readHandler(const boost::system::error_code& error, size_t bytesTransferred) {

    //do something on reading from the client
    std::cout<<"[server] read something from client"<<std::endl;
}

void Session::writeHandler(const boost::system::error_code& error,  size_t bytesTransferred) {

    //do something on write
    std::cout<<"[server] written something from the client"<<std::endl;
}

void Session::receiveMessage() {
    async_read(m_socket, buffer(m_acceptMessage), std::bind(&Session::readHandler, std::shared_from_this(),
                placeholders::error, placeholders::bytes_transferred));
}

void Session::sendMessage(const std::string& message) {
    async_write(m_socket, buffer(message), std::bind(&Session::writeHandler, std::shared_from_this()),
            placeholders::error, placeholders::bytes_transferred);
}


ip::tcp& Session::getSocket(){
    return m_socket;
}

0 个答案:

没有答案