簡體   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