无法访问类'boost :: asio :: basic_io_object <ioobjectservice> </ioobjectservice>中声明的私有成员

时间:2013-09-12 21:53:00

标签: c++ sockets boost boost-asio

boost::asio::basic_io_object<IoObjectService>::basic_io_object' : cannot access private member declared in class 'boost::asio::basic_io_object<IoObjectService>

它没有告诉这个错误发生在哪里:|,这是我的代码

AsyncConnection

#ifndef _ASYNCCONNECTION_H_
#define _ASYNCCONNECTION_H_

#include <boost\shared_ptr.hpp>
#include <boost\enable_shared_from_this.hpp>
#include <boost\bind.hpp>
#include <boost\asio.hpp>

class AsyncConnection : public boost::enable_shared_from_this<AsyncConnection>
{
public:
    typedef boost::shared_ptr<AsyncConnection> Pointer;
    explicit AsyncConnection(boost::asio::ip::tcp::socket& socket);
    virtual ~AsyncConnection();

    virtual void BeginReceive();
    virtual void EndReceive(const boost::system::error_code& error, std::size_t bytes_transferred);

    boost::asio::ip::tcp::socket& GetSocket();
private:
    boost::asio::ip::tcp::socket m_socket;
    char buffer[1024];
};

#endif


#include "AsyncConnection.h"


AsyncConnection::AsyncConnection(boost::asio::ip::tcp::socket& socket)
    : m_socket(socket)
{
}


AsyncConnection::~AsyncConnection()
{
}


boost::asio::ip::tcp::socket& AsyncConnection::GetSocket(){
    return m_socket;
}

void AsyncConnection::BeginReceive(){

    boost::asio::async_read(socket, boost::asio::buffer(buffer),
        boost::asio::transfer_at_least(1),
        boost::bind(&AsyncConnection::EndReceive, shared_from_this(),
        boost::asio::placeholders::error,
        boost::asio::placeholders::bytes_transferred));
}

void AsyncConnection::EndReceive(const boost::system::error_code& error, std::size_t bytes_transferred){
    if (!error){

    }
    else{
        std::cout << error.message() << std::endl;
    }
}

IAsyncConnectionFactory

#ifndef _IASYNCCONNECTIONFACTORY_H_
#define _IASYNCCONNECTIONFACTORY_H_

#include "AsyncConnection.h"

class IAsyncConnectionFactory
{
public:
    IAsyncConnectionFactory();
    virtual ~IAsyncConnectionFactory();

    virtual AsyncConnection::Pointer Create(boost::asio::ip::tcp::socket& socket) = 0;
};

#endif

AsyncServer

#pragma once
#ifndef _ASYNCSERVER_H_
#define _ASYNCSERVER_H_

#include <string>
#include <boost/bind.hpp>
#include "IAsyncConnectionFactory.h"

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

class AsyncServer
{
public:
    AsyncServer(boost::asio::io_service& io_service, std::string ip, unsigned short port, boost::shared_ptr < IAsyncConnectionFactory> factory);
    ~AsyncServer();

    void BeginAccept();
    void EndAccept(AsyncConnection::Pointer connection, const boost::system::error_code& error);
private:
    boost::shared_ptr < IAsyncConnectionFactory> m_factory;
    tcp::acceptor acceptor;
};

#endif

#include "AsyncServer.h"


AsyncServer::AsyncServer(boost::asio::io_service& io_service, std::string ip, unsigned short port, boost::shared_ptr< IAsyncConnectionFactory> factory)
    : acceptor(io_service, tcp::endpoint(boost::asio::ip::address_v4::from_string(ip), port)), m_factory(factory)
{

    BeginAccept();
}


AsyncServer::~AsyncServer()
{

}


void AsyncServer::BeginAccept(){
    AsyncConnection::Pointer new_connection = m_factory->Create(boost::asio::ip::tcp::socket(acceptor.get_io_service()));

    acceptor.async_accept(new_connection->GetSocket(),
        boost::bind(&AsyncServer::EndAccept, this, new_connection, boost::asio::placeholders::error));
}

void AsyncServer::EndAccept(AsyncConnection::Pointer connection, const boost::system::error_code& error){
    if (!error)
    {
        connection->BeginReceive();
    }
    else
    {
        std::cout << error.message() << std::endl;
    }

    BeginAccept();
}

1 个答案:

答案 0 :(得分:2)

您的GetSocket成员函数尝试按值返回tcp::socket。这是不可能的,因为tcp::socket无法复制。

您可以返回tcp::socket &,但请务必避免悬挂引用。

更新:如果仍然出现此错误,请确保永远不会尝试复制包含Asio对象的对象。要获得更好的诊断功能,请在operator =AsyncConnection中创建copy-ctor和AsyncServer私有 - 然后编译器将准确指向发生此类复制的位置。

Update2 :我错过了另一点:您的AsyncConnection构造函数尝试复制tcp::socket。通过引用存储套接字或使用移动语义(在C ++ 11中)。