gpt4 book ai didi

c++ - cpp-netlib hello world 示例不会在 linux 上编译

转载 作者:太空宇宙 更新时间:2023-11-04 12:26:11 33 4
gpt4 key购买 nike

我尝试在 Ubuntu 16.04 上使用 cpp-netlib-0.12.0 和 boost-1.64.0 在 cpp-netlib.org 上运行 hello world 示例。一段代码是(来自第 1 行):

#include <boost/network/protocol/http/server.hpp>
#include <iostream>

namespace http = boost::network::http;

struct hello_world;
typedef http::server<hello_world> server;

struct hello_world
{
void operator()(server::request const &request, server::response &response)
{
server::string_type ip = source(request);
unsigned int port = request.source_port;
std::ostringstream data;
data << "Hello, " << ip << ':' << port << '!';
response = server::response::stock_reply(server::response::ok, data.str());
}
void log(const server::string_type& message)
{
std::cerr << "ERROR: " << message << std::endl;
}
};

当我使用以下行进行编译时:

g++ test1.cpp -o test1 -std=c++11 -lcppnetlib-uri -lcppnetlib-server-parsers -lcppnetlib-client-connections -lboost_system -lboost_thread -lpthread

我收到以下错误:

test1.cpp:11:61: error: ‘boost::network::http::server<hello_world>::response’ has not been declared
void operator()(server::request const &request, server::response &response)
^
test1.cpp: In member function ‘void hello_world::operator()(const request&, int&)’:
test1.cpp:17:28: error: ‘boost::network::http::server<hello_world>::response’ has not been declared
response = server::response::stock_reply(server::response::ok, data.str());
^
test1.cpp:17:58: error: ‘boost::network::http::server<hello_world>::response’ has not been declared
response = server::response::stock_reply(server::response::ok, data.str());
^

我直接从网站上的示例中提取了代码。我检查了包含路径,所有必要的库似乎都在那里。我似乎无法弄清楚问题出在哪里。

最佳答案

看起来 cpp-netlib 的文档都乱七八糟(就像整个项目一样)。 HTTP server API page坚持 http::serverHandler 模板参数应该有成员 operator ()connection_ptr 作为第二个参数(虽然它建议错误的包括):

#include <boost/network/protocol/http/server.hpp>
#include <boost/network/utils/thread_pool.hpp>

struct handler_type;
typedef boost::network::http::server<handler_type> http_server;

struct handler_type
{
void
operator ()
(
http_server::request const & request,
http_server::connection_ptr connection
)
{
// do something here
}
};

关于c++ - cpp-netlib hello world 示例不会在 linux 上编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44600070/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com