首页 > 解决方案 > 在 stream_descriptor::async_wait 中无效使用非静态成员函数

问题描述

Debian Linux,升压 1.67。以 boost on encoding boost::asio::posix::stream_descriptor::async_wait 为例,并将其放入一个类中,我在 async_wait 行的 wait_handler 上得到“无效使用非静态成员函数”。

提升示例

void wait_handler(const boost::system::error_code& error)
{
  if (!error)
  {
    // Wait succeeded.
  }
}

...

boost::asio::posix::stream_descriptor descriptor(io_context);
...
descriptor.async_wait(
    boost::asio::posix::stream_descriptor::wait_read,
    wait_handler);

转换成

void EHandler::wait_handler(const boost::system::error_code& error)
{
  if (!error)
  {
    // Wait succeeded.
  }
}


void EHandler::StartRead()
{
  ...
  boost::asio::posix::stream_descriptor descriptor(io_context);

  descriptor.async_wait(boost::asio::posix::stream_descriptor::wait_read, wait_handler);
}

试过:

... wait_handler, this);
... EHandler::wait_handler);
... &EHandler::wait_handler);
... EHandler::wait_handler, this);
... &EHandler::wait_handler, this);
... boost::bind() with each of the above options

甚至尝试将 _1) 添加到每个。我在看什么?

由于此例程将被最多 8 个线程调用,因此不能将其声明为静态,这只是对错误消息解决方案的建议。

标签: c++boostboost-asio

解决方案


尚未完全测试,这只是通过编译器错误。

descriptor.async_wait(boost::asio::posix::stream_descriptor::wait_read, boost::bind(&EHandler::wait_handler, this, boost::asio::placeholders::error));


推荐阅读