【发布时间】: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 <boost/asio/io_context.hpp>
标签: c++ boost-asio asio