C ++ требует спецификатора типа для всех заявлений - PullRequest
0 голосов
/ 13 марта 2020

У меня есть http-сервер, как показано ниже.

#pragma once

#include <boost/property_tree/json_parser.hpp>
#include <boost/beast/core.hpp>
#include <boost/beast/http.hpp>
#include <boost/beast/version.hpp>
#include <boost/asio.hpp>
#include <chrono>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <memory>
#include <string>

namespace beast = boost::beast;
namespace http = beast::http;
namespace net = boost::asio;
using tcp = boost::asio::ip::tcp;

class RESTServer : public std::enable_shared_from_this<RESTServer> {
public:
    RESTServer(tcp::socket socket)
        : m_socket(std::move(socket)) {
    }

    void start() {
        readRequest();
        checkDeadline();
    }

private:
    tcp::socket m_socket;
    beast::flat_buffer m_buffer{8192};
    http::request<http::dynamic_body> m_request;
    http::response<http::dynamic_body> m_response;

    net::steady_timer m_deadline{m_socket.get_executor(), std::chrono::seconds(60)};

    void readRequest() {
        auto self = shared_from_this();
        http::async_read(m_socket, m_buffer, m_request, [self](beast::error_code ec, std::size_t bytes_transferred) {
            boost::ignore_unused(bytes_transferred);
            if (!ec) {
                self->processRequest();
            }
        });
    }

    void processRequest() {
        m_response.version(m_request.version());
        m_response.keep_alive(false);

        switch (m_request.method()) {
            case http::verb::get:
                m_response.result(http::status::ok);
                m_response.set(http::field::server, "Beast");
                createResponse();
                break;

            case http::verb::post:
                m_response.result(http::status::ok);
                m_response.set(http::field::server, "Beast");
                createResponse();
                break;

            default:
                m_response.result(http::status::bad_request);
                m_response.set(http::field::content_type, "text/plain");
                beast::ostream(m_response.body())
                    << "Invalid request-method '"
                    << std::string(m_request.method_string())
                    << "'";
                break;
        }
        writeResponse();
    }

    void createResponse() {
        if(request_.target() == "/count")
        {
            response_.set(http::field::content_type, "text/html");
            beast::ostream(response_.body())
                << "<html>\n"
                <<  "<head><title>Request count</title></head>\n"
                <<  "<body>\n"
                <<  "<h1>Request count</h1>\n"
                <<  "<p>There have been "
                <<  my_program_state::request_count()
                <<  " requests so far.</p>\n"
                <<  "</body>\n"
                <<  "</html>\n";
        }
        else if(request_.target() == "/time")
        {
            response_.set(http::field::content_type, "text/html");
            beast::ostream(response_.body())
                <<  "<html>\n"
                <<  "<head><title>Current time</title></head>\n"
                <<  "<body>\n"
                <<  "<h1>Current time</h1>\n"
                <<  "<p>The current time is "
                <<  my_program_state::now()
                <<  " seconds since the epoch.</p>\n"
                <<  "</body>\n"
                <<  "</html>\n";
        }
        else
        {
            response_.result(http::status::not_found);
            response_.set(http::field::content_type, "text/plain");
            beast::ostream(response_.body()) << "File not found\r\n";
        }
    }

    void writeResponse() {
        auto self = shared_from_this();

        m_response.set(http::field::content_length, m_response.body().size());

        http::async_write(m_socket, m_response,
                          [self](beast::error_code ec, std::size_t) {
                              self->m_socket.shutdown(tcp::socket::shutdown_send, ec);
                              self->m_deadline.cancel();
                          });
    }

    void checkDeadline() {
        auto self = shared_from_this();

        m_deadline.async_wait([self](beast::error_code ec) {
            if (!ec) {
                self->m_socket.close(ec);
            }
        });
    }
};

void httpServer(tcp::acceptor& acceptor, tcp::socket& socket) {
    acceptor.async_accept(socket, [&](beast::error_code ec) {
        if (!ec) {
            std::make_shared<RESTServer>(std::move(socket))->start();
        }
        httpServer(acceptor, socket);
    });
}

У меня есть это в отдельном файле RESTServer.hpp. В моем классе TestRestClient я пытаюсь создать сервер следующим образом.

class MyTest : public ::testing::Test{
    net::ip::address m_address = net::ip::make_address("0.0.0.0");
    unsigned short m_port = static_cast<unsigned short>(8080);
    net::io_context m_ioc{1};

    tcp::acceptor m_acceptor{m_ioc, {m_address, m_port}};
    tcp::socket m_socket{m_ioc};

    httpServer(m_acceptor, m_socket);

};

Я получаю красную волнистую линию под httpServer. Я вижу предупреждение C++ requires type specifier for all declerations.

Что я делаю не так? Как мне это исправить?

...