【问题标题】:namespace "asio" has no member "io_context"命名空间“asio”没有成员“io_context”
【发布时间】:2020-11-18 04:58:20
【问题描述】:

我正在试图弄清楚为什么当我关注的视频我做了完全相同的事情但它对他有用时会这样说。我要解决的错误是命名空间“asio”没有成员“io_context” Code

#include <iostream>

#ifdef _WIN32
#define _WIN32_WINNT 0x0A00
#endif
#define ASIO_STANDALONE // asio came out of the boost development framework but
// I am not using it so it tells asio it should be used as standalone.
#include <asio.hpp>
#include <asio/ts/buffer.hpp> // Handle the movement of memory
#include <asio/ts/internet.hpp> // Prepares asio for all the things I need to do network communication

int main()
{
    asio::error_code ec;

    // Create a "context" - essentially the platform specific interface
    asio::io_context context;

    // Get address of where I want to connect to
    asio::ip:tcp::endpoint endpoint(asio::ip::make_address("127.0.0.1", ec), 80);

    // Create a socket, the context will deliver the implementation
    asio::ip::tcp::socket socket(context);

    // Tell socket to connect to address
    socket.connect(endpoint, ec);

    // Checks for connection status (Connected/Failed)
    if (!ec) {
        std::cout << "Connected!" << std::endl;
    }
    else
    {
        std::cout << "Failed to connect to:\n" << ec.message() << std::endl;
    }

    system("pause");
    return 0;
}

【问题讨论】:

  • 您能否完整地逐字显示您的第一个编译错误?
  • io_context 似乎仍然在 boost::asio 命名空间中,无论您是否单独使用它。你为什么要离开boost
  • 另外请说明您使用的是哪个版本的 boost 或 asio。
  • 你需要#include &lt;boost/asio/io_context.hpp&gt;

标签: c++ boost-asio asio


【解决方案1】:

您的 boost 版本使用旧界面 (io_service)。只需升级或使用旧界面即可。

对比升级说明:https://www.boost.org/doc/libs/1_74_0/doc/html/boost_asio/net_ts.html

这是您在 Boost 1.66 下的 sn-p:

Live Om Wandbox

#include <iostream>

#ifdef _WIN32
#define _WIN32_WINNT 0x0A00
#endif
#define ASIO_STANDALONE // asio came out of the boost development framework but
// I am not using it so it tells asio it should be used as standalone.
#include <boost/asio.hpp>

namespace asio = boost::asio;
using boost::system::error_code;

int main() {
    error_code ec;
    asio::io_service context;
    asio::ip::tcp::endpoint endpoint(asio::ip::make_address("127.0.0.1", ec), 80);

    asio::ip::tcp::socket socket(context);
    socket.connect(endpoint, ec);

    if (!ec) {
        std::cout << "Connected!" << std::endl;
    } else {
        std::cout << "Failed to connect to:\n" << ec.message() << std::endl;
    }
}

打印

Connected!

更现代:

使用 Boost 1.73

Live On Wandbox

#include <boost/asio.hpp>
#include <iostream>
using boost::asio::ip::tcp;

int main() {
    try {
        tcp::socket socket(boost::asio::system_executor{});
        socket.connect({{}, 80});

        std::cout << "Connected!" << std::endl;
    } catch (std::exception const& e) {
        std::cout << "Failed to connect to:\n" << e.what() << std::endl;
    }
}

还打印“已连接!”。 (当然除非在魔杖盒上)

【讨论】:

  • 添加了一个“老式 1.66.0”版本以及一个非常简化的 1.73.0 版本。 [现场演示]
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-04-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-16
  • 2021-06-05
  • 2021-04-05
相关资源
最近更新 更多