Boost Library

时间:2017-12-19 21:34:40

标签: c++ boost

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

  

io_context不是asio的成员。

我构建了Boost 4次,我想也许问题可能是由于我的安装失败,但似乎不是。

#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();
    }

1 个答案:

答案 0 :(得分:12)

事情changed in Boost 1.66:

enter image description here

release notes显示重命名/更改的界面:

  

Boost.Asio现在提供“C ++扩展网络”技术规范指定的接口和功能。除了通过通常的Boost.Asio头文件进行访问之外,还可以通过与TS中定义的头文件相对应的特殊头来访问此功能。这些列在下表中:

enter image description here