gpt4 book ai didi

c++ - Boost 库中的 Io_context 错误

转载 作者:可可西里 更新时间:2023-11-01 16:34:05 27 4
gpt4 key购买 nike

我正在尝试使用 Boost Libraries 构建聊天室。但是当我尝试使用 asio::io_context 时,编译器说:

io_context is not an member of asio.

我构建了 4 次 Boost,我认为问题可能是由于我的安装失败造成的,但事实并非如此。

#include <ctime>
#include <iostream>
#include <string>
#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/asio.hpp>

using boost::asio::ip::tcp;

std::string make_daytime_string()
{
using namespace std; // For time_t, time and ctime;
time_t now = time(0);
return ctime(&now);
}

class tcp_connection
: public boost::enable_shared_from_this<tcp_connection>
{
public:
typedef boost::shared_ptr<tcp_connection> pointer;

static pointer create(boost::asio::io_context& io_context)
{
return pointer(new tcp_connection(io_context));
}

tcp::socket& socket()
{
return socket_;
}

void start()
{
message_ = make_daytime_string();

boost::asio::async_write(socket_, boost::asio::buffer(message_),
boost::bind(&tcp_connection::handle_write, shared_from_this(),
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}

private:
tcp_connection(boost::asio::io_context& io_context)
: socket_(io_context)
{
}

void handle_write(const boost::system::error_code& /*error*/,
size_t /*bytes_transferred*/)
{
}

tcp::socket socket_;
std::string message_;
};

class tcp_server
{
public:
tcp_server(boost::asio::io_context& io_context) //error
: acceptor_(io_context, tcp::endpoint(tcp::v4(), 13)) //error
{
start_accept();
}

最佳答案

东西changed in Boost 1.66:

enter image description here

release notes显示重命名/更改的接口(interface):

Boost.Asio now provides the interfaces and functionality specified by the "C++ Extensions for Networking" Technical Specification. In addition to access via the usual Boost.Asio header files, this functionality may be accessed through special headers that correspond to the header files defined in the TS. These are listed in the table below:

enter image description here

关于c++ - Boost 库中的 Io_context 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47895520/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com