繁体   English   中英

错误:“async_read_until”不是“boost::asio”的成员

[英]error: ‘async_read_until’ is not a member of ‘boost::asio’

代码: https://coliru.stacked-crooked.com/a/d39bdc13f47785e5

参考:

https://www.boost.org/doc/libs/1_72_0/libs/beast/doc/html/beast/using_io/timeouts.html https://www.boost.org/doc/libs/1_72_0/doc/html /boost_asio/reference/async_read_until.html

/** This function echoes back received lines from a peer, with a timeout.

    The algorithm terminates upon any error (including timeout).
*/
template <class Protocol, class Executor>
void do_async_echo (basic_stream<Protocol, Executor>& stream)
{
    // This object will hold our state when reading the line.

    struct echo_line
    {
        basic_stream<Protocol, Executor>& stream;

        // The shared pointer is used to extend the lifetime of the
        // string until the last asynchronous operation completes.
        std::shared_ptr<std::string> s;

        // This starts a new operation to read and echo a line
        void operator()()
        {
            // If a line is not sent and received within 30 seconds, then
            // the connection will be closed and this algorithm will terminate.

            stream.expires_after(std::chrono::seconds(30));

            // Read a line from the stream into our dynamic buffer, with a timeout
            net::async_read_until(stream, net::dynamic_buffer(*s), '\n', std::move(*this));
        }

        // This function is called when the read completes
        void operator()(error_code ec, std::size_t bytes_transferred)
        {
            if(ec)
                return;

            net::async_write(stream, buffers_prefix(bytes_transferred, net::buffer(*s)),
                [this](error_code ec, std::size_t bytes_transferred)
                {
                    s->erase(s->begin(), s->begin() + bytes_transferred);

                    if(! ec)
                    {
                        // Run this algorithm again
                        echo_line{stream, std::move(s)}();
                    }
                    else
                    {
                        std::cerr << "Error: " << ec.message() << "\n";
                    }
                });
        }
    };

    // Create the operation and run it
    echo_line{stream, std::make_shared<std::string>()}();
}


$ g++ -std=gnu++17 -I ./boost/boost_1_72_0. timeout_example.cpp -o ./build/timeout_example
timeout_example.cpp: In member function ‘void do_async_echo(boost::beast::basic_stream<Protocol, Executor>&)::echo_line::operator()()’:
timeout_example.cpp:43:18: error: ‘async_read_until’ is not a member of ‘boost::asio’; did you mean ‘async_result’?
   43 |             net::async_read_until(stream, net::dynamic_buffer(*s), '\n', std::move(*this));
      |                  ^~~~~~~~~~~~~~~~
      |                  async_result

问题> 为什么编译器(g++ (GCC) 10.2.1) 找不到boost::asio::async_read_until

谢谢

您缺少正确的 header 文件。 包括

#include <boost/asio/read_until.hpp>

或者

#include <boost/asio.hpp>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM