cpp-netlib你好世界的例子不会在linux

时间:2017-06-17 01:55:34

标签: c++ linux cpp-netlib

我尝试使用cpp-netlib-0.12.0和Ubuntu 16.04上的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());
                                                          ^

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

1 个答案:

答案 0 :(得分:0)

看起来cpp-netlib的文档都搞砸了(就像整个项目一般)。 HTTP server API page坚持认为Handler的{​​{1}}模板参数应该让成员http::serveroperator ()作为第二个参数(尽管它暗示错误包含):

connection_ptr