简体   繁体   中英

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

Code: https://coliru.stacked-crooked.com/a/d39bdc13f47785e5

Reference:

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

Question> Why does the compiler(g++ (GCC) 10.2.1) cannot find the boost::asio::async_read_until ?

Thank you

You are missing the correct header file. Include

#include <boost/asio/read_until.hpp>

or

#include <boost/asio.hpp>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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