Abanoub
Abanoub

Reputation: 3809

boost::asio async_send error

The following code using boost::asio will not compile:

#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 ){
  }

These are the errors this code produces in 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

Why are these errors occurring, and what can I do to fix them?

Upvotes: 1

Views: 1577

Answers (2)

Alok Save
Alok Save

Reputation: 206656

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

This first error tells you that: To take the 'address' of a member function you need to use the following syntax:

&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

The second error tells you that there is mismatch in the number of arguments you are passing to the function

So the Correct way to call async_send is

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

Upvotes: 4

SoapBox
SoapBox

Reputation: 20609

Your call to async_send should look like this:

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

Upvotes: 2

Related Questions