boost::asio async_send 错误

发布于 2024-11-09 01:33:32 字数 2114 浏览 3 评论 0原文

使用 boost::asio 的以下代码将无法编译:

#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

为什么会发生这些错误,我该如何修复它们?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半寸时光 2024-11-16 01:33:32
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

第二个错误告诉您传递给函数的参数数量不匹配。 所以调用

async_send的正确方法是

socket_.async_send(boost::asio::buffer(buffer, length), boost::bind(&Connection::handler, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
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));
遮了一弯 2024-11-16 01:33:32

您对 async_send 的调用应如下所示:

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

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));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文