2011-05-21 155 views
1

使用升壓下面的代碼:: ASIO不會編譯:的boost :: ASIO async_send錯誤

#pragma once 
#include <ctime> 
#include <iostream> 
#include <string> 
#include <stdint.h> 
#include <boost/bind.hpp> 
#include <boost/shared_ptr.hpp> 
#include <boost/enable_shared_from_this.hpp> 
#include <boost/asio.hpp> 

class Connection 
: public boost::enable_shared_from_this<Connection> 
{ 
public: 
    typedef boost::shared_ptr<Connection> pointer; 
    static pointer create(boost::asio::io_service& io_service){return pointer(new Connection(io_service));} 
    virtual ~Connection(); 
    boost::asio::ip::tcp::socket& socket(); 

    virtual void Send(uint8_t* buffer, int length); 
    void handler(const boost::system::error_code& error, std::size_t bytes_transferred); 

private: 
    explicit Connection(boost::asio::io_service& io_service); 
    boost::asio::ip::tcp::socket socket_; 
    boost::asio::ip::tcp::endpoint remote_endpoint_; 
}; 


------------------------------------------------------------------------------------------------ 
#include "Connection.h" 

Connection::Connection(boost::asio::io_service& io_service) 
    : socket_(io_service) 
    { 
    } 

Connection::~Connection() 
{ 
    //dtor 
} 


boost::asio::ip::tcp::socket& Connection::socket(){ 
    return socket_; 
} 

void Connection::Send(uint8_t* buffer, int length){ 
    socket_.async_send(boost::asio::buffer(buffer, length), 0, handler); 

} 
       // Result of operation.    // Number of bytes sent. 
void Connection::handler(const boost::system::error_code& error, std::size_t bytes_transferred){ 
    } 

這些都是該代碼會產生在Visual C++中的錯誤:

Error 1 error C3867: 'Connection::handler': function call missing argument list; use '&Connection::handler' to create a pointer to member d:\c++\ugs\accountserver\connection.cpp 19 
Error 2 error C2780: 'void boost::asio::basic_stream_socket<Protocol>::async_send(const ConstBufferSequence &,WriteHandler)' : expects 2 arguments - 3 provided d:\c++\ugs\accountserver\connection.cpp 19 

爲什麼這些錯誤發生,我能做些什麼來解決它們?

回答

2

您對async_send通話應該是這樣的:

socket_.async_send(boost::asio::buffer(buffer, length), boost::bind(&Connection::handler, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)); 
4
Error 1 error C3867: 'Connection::handler': function call missing argument list; use '&Connection::handler' to create a pointer to member d:\c++\ugs\accountserver\connection.cpp 19 

這第一個錯誤告訴你: 帶你需要使用下面的語法的一個成員函數的「地址」:

&Connection::handler

Error 2 error C2780: 'void boost::asio::basic_stream_socket<Protocol>::async_send(const ConstBufferSequence &,WriteHandler)' : expects 2 arguments - 3 provided d:\c++\ugs\accountserver\connection.cpp 19 

第二個錯誤告訴你您傳遞給函數的參數數量不匹配

因此,調用的正確方法是

socket_.async_send(boost::asio::buffer(buffer, length), boost::bind(&Connection::handler, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));