简体   繁体   中英

The sample code I got from cpp-netlib won't compile

// 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;
}
//]

I suspect it's something to do with not linking my libraries correctly. This is the error I get when I run make. I can run this example fine off the cpp-netlib folder but when I try to copy that code and put it in my own folder, it doesn't compile.

/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

Note in the CMakeLists.txt under root directory, there is 1 line for Asio

include_directories(deps/asio/asio/include)

add it to your project, then it will work

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