gpt4 book ai didi

c++ - 我从 cpp-netlib 获得的示例代码无法编译

转载 作者:行者123 更新时间:2023-11-28 06:02:09 25 4
gpt4 key购买 nike

// Copyright 2009 (c) Tarro, Inc.
// Copyright 2009 (c) Dean Michael Berris <mikhailberis@gmail.com>
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//

//[ hello_world_server_main
/*`
This is a part of the 'Hello World' example. It's used to
demonstrate how easy it is to set up an HTTP server. All we do in
this example is create a request handler and run the server.
*/
#include <boost/network/protocol/http/server.hpp>
#include <iostream>


namespace http = boost::network::http;


/*<< Defines the server. >>*/
struct hello_world;
typedef http::server<hello_world> server;

/*<< Defines the request handler. It's a class that defines two
functions, `operator()` and `log()` >>*/
struct hello_world {
/*<< This is the function that handles the incoming request. >>*/
void operator() (server::request const &request,
server::response &response) {
server::string_type ip = source(request);
std::ostringstream data;
data << "Hello, " << ip << "!";
response = server::response::stock_reply(
server::response::ok, data.str());
}
/*<< It's necessary to define a log function, but it's ignored in
this example. >>*/
void log(...) {
// do nothing
}
};


int main(int argc, char * argv[]) {

if (argc != 3) {
std::cerr << "Usage: " << argv[0] << " address port" << std::endl;
return 1;
}

try {
/*<< Creates the request handler. >>*/
hello_world handler;
/*<< Creates the server. >>*/
server server_(argv[1], argv[2], handler);
/*<< Runs the server. >>*/
server_.run();
}
catch (std::exception &e) {
std::cerr << e.what() << std::endl;
return 1;
}

return 0;
}
//]

我怀疑这与没有正确链接我的库有关。这是我运行 make 时遇到的错误。我可以在 cpp-netlib 文件夹下正常运行这个示例,但是当我尝试复制该代码并将其放入我自己的文件夹时,它无法编译。

/home/stanley/cmpt373/textadventure/tools/simple_server_test/simpleServerTest.cpp: In function ‘int main(int, char**)’:
/home/stanley/cmpt373/textadventure/tools/simple_server_test/simpleServerTest.cpp:56:49: error: no matching function for call to ‘boost::network::http::server<hello_world>::server(char*&, char*&, hello_world&)’
server server_(argv[1], argv[2], handler);
^
/home/stanley/cmpt373/textadventure/tools/simple_server_test/simpleServerTest.cpp:56:49: note: candidate is:
In file included from /home/stanley/cmpt373/textadventure/tools/simple_server_test/simpleServerTest.cpp:14:0:
/home/stanley/cmpt373/textadventure/include/boost/network/protocol/http/server.hpp:44:12: note: boost::network::http::server<Handler>::server(const options&) [with Handler = hello_world; boost::network::http::server<Handler>::options = boost::network::http::server_options<boost::network::http::tags::http_server, hello_world>]
explicit server(options const &options) : server_base(options) {}
^
/home/stanley/cmpt373/textadventure/include/boost/network/protocol/http/server.hpp:44:12: note: candidate expects 1 argument, 3 provided
make[2]: *** [tools/simple_server_test/CMakeFiles/simpleServerTest.dir/simpleServerTest.cpp.o] Error 1
make[1]: *** [tools/simple_server_test/CMakeFiles/simpleServerTest.dir/all] Error 2
make: *** [all] Error 2

最佳答案

注意在根目录下的CMakeLists.txt中,有1行是Asio

include_directories(deps/asio/asio/include)

将其添加到您的项目中,然后它将起作用

关于c++ - 我从 cpp-netlib 获得的示例代码无法编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33050929/

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