boost :: asio :: ip :: udp :: socket creation - 编译错误 - 调用boost :: asio :: ip :: udp :: socket(* my_io_service,server_endpoint);

时间:2018-05-15 02:35:26

标签: boost-asio

使用boost.asio编译boost库时遇到问题。

我正在尝试从类构造函数创建一个boost-udp套接字。我尝试了几个不同的东西来编译,但我仍然看到了这个问题。

有没有人对这个问题有所了解?任何人都可以帮我解决这个错误,我在做什么?

这是我的班级:

    class NetworkTestToolServer
    {
        private:
            boost::asio::io_service*       my_io_service;
            boost::asio::ip::udp::endpoint server_endpoint;
            boost::asio::ip::udp::socket   server_udp_socket;
            boost::asio::ip::udp::endpoint sender_endpoint;
            boost::asio::ip::udp::endpoint client_endpoint;
        public:
            NetworkTestToolServer(short port);
    };

这是我的构造函数,我正在使用(在server_udp_socket失败):

NetworkTestToolServer::NetworkTestToolServer(short port)
{
    my_io_service = new boost::asio::io_service();

    server_endpoint = boost::asio::ip::udp::endpoint(boost::asio::ip::udp::v4(),
                                                     50002);

    server_udp_socket = boost::asio::ip::udp::socket(*my_io_service, 
                                                     server_endpoint);
}

我看到以下错误:

user2@user2-VirtualBox:~/network_test_tool/network-test-tool/build$ make
[ 20%] Built target gmock_main
[ 35%] Built target gmock
[ 45%] Built target gtest
[ 55%] Built target gtest_main
Scanning dependencies of target network-test-tool-lib
[ 60%] Building CXX object app/CMakeFiles/network-test-tool-lib.dir/src/ntt/NetworkTestToolServer.cpp.o                                                                                         
/home/user2/network_test_tool/network-test-tool/app/src/ntt/NetworkTestToolServer.cpp: In constructor ‘ntt::NetworkTestToolServer::NetworkTestToolServer(short int)’:
/home/user2/network_test_tool/network-test-tool/app/src/ntt/NetworkTestToolServer.cpp:34:56: error: no matching function for call to ‘boost::asio::basic_datagram_socket<boost::asio::ip::udp>::basic_datagram_socket()’
 NetworkTestToolServer::NetworkTestToolServer(short port)
                                                        ^
In file included from /usr/include/boost/asio.hpp:21:0,
                 from /home/user2/network_test_tool/network-test-tool/app/src/ntt/NetworkTestToolServer.cpp:3:
/usr/include/boost/asio/basic_datagram_socket.hpp:183:3: note: candidate: template<class Protocol1, class DatagramSocketService1> boost::asio::basic_datagram_socket<Protocol, DatagramSocketService>::basic_datagram_socket(boost::asio::basic_datagram_socket<Protocol1, DatagramSocketService1>&&, typename std::enable_if<std::is_convertible<Protocol1, Protocol>::value>::type*)
   basic_datagram_socket(
   ^~~~~~~~~~~~~~~~~~~~~
/usr/include/boost/asio/basic_datagram_socket.hpp:183:3: note:   template argument deduction/substitution failed:
/home/user2/network_test_tool/network-test-tool/app/src/ntt/NetworkTestToolServer.cpp:34:56: note:   candidate expects 2 arguments, 0 provided
 NetworkTestToolServer::NetworkTestToolServer(short port)
                                                        ^
In file included from /usr/include/boost/asio.hpp:21:0,
                 from /home/user2/network_test_tool/network-test-tool/app/src/ntt/NetworkTestToolServer.cpp:3:
/usr/include/boost/asio/basic_datagram_socket.hpp:147:3: note: candidate: boost::asio::basic_datagram_socket<Protocol, DatagramSocketService>::basic_datagram_socket(boost::asio::basic_datagram_socket<Protocol, DatagramSocketService>&&) [with Protocol = boost::asio::ip::udp; DatagramSocketService = boost::asio::datagram_socket_service<boost::asio::ip::udp>]
   basic_datagram_socket(basic_datagram_socket&& other)
   ^~~~~~~~~~~~~~~~~~~~~
/usr/include/boost/asio/basic_datagram_socket.hpp:147:3: note:   candidate expects 1 argument, 0 provided
/usr/include/boost/asio/basic_datagram_socket.hpp:129:3: note: candidate: boost::asio::basic_datagram_socket<Protocol, DatagramSocketService>::basic_datagram_socket(boost::asio::io_service&, const protocol_type&, const native_handle_type&) [with Protocol = boost::asio::ip::udp; DatagramSocketService = boost::asio::datagram_socket_service<boost::asio::ip::udp>; boost::asio::basic_datagram_socket<Protocol, DatagramSocketService>::protocol_type = boost::asio::ip::udp; boost::asio::basic_datagram_socket<Protocol, DatagramSocketService>::native_handle_type = int]
   basic_datagram_socket(boost::asio::io_service& io_service,
   ^~~~~~~~~~~~~~~~~~~~~
/usr/include/boost/asio/basic_datagram_socket.hpp:129:3: note:   candidate expects 3 arguments, 0 provided
/usr/include/boost/asio/basic_datagram_socket.hpp:108:3: note: candidate: boost::asio::basic_datagram_socket<Protocol, DatagramSocketService>::basic_datagram_socket(boost::asio::io_service&, const endpoint_type&) [with Protocol = boost::asio::ip::udp; DatagramSocketService = boost::asio::datagram_socket_service<boost::asio::ip::udp>; boost::asio::basic_datagram_socket<Protocol, DatagramSocketService>::endpoint_type = boost::asio::ip::basic_endpoint<boost::asio::ip::udp>]
   basic_datagram_socket(boost::asio::io_service& io_service,
   ^~~~~~~~~~~~~~~~~~~~~
/usr/include/boost/asio/basic_datagram_socket.hpp:108:3: note:   candidate expects 2 arguments, 0 provided
/usr/include/boost/asio/basic_datagram_socket.hpp:86:3: note: candidate: boost::asio::basic_datagram_socket<Protocol, DatagramSocketService>::basic_datagram_socket(boost::asio::io_service&, const protocol_type&) [with Protocol = boost::asio::ip::udp; DatagramSocketService = boost::asio::datagram_socket_service<boost::asio::ip::udp>; boost::asio::basic_datagram_socket<Protocol, DatagramSocketService>::protocol_type = boost::asio::ip::udp]
   basic_datagram_socket(boost::asio::io_service& io_service,
   ^~~~~~~~~~~~~~~~~~~~~
/usr/include/boost/asio/basic_datagram_socket.hpp:86:3: note:   candidate expects 2 arguments, 0 provided
/usr/include/boost/asio/basic_datagram_socket.hpp:69:12: note: candidate: boost::asio::basic_datagram_socket<Protocol, DatagramSocketService>::basic_datagram_socket(boost::asio::io_service&) [with Protocol = boost::asio::ip::udp; DatagramSocketService = boost::asio::datagram_socket_service<boost::asio::ip::udp>]
   explicit basic_datagram_socket(boost::asio::io_service& io_service)
            ^~~~~~~~~~~~~~~~~~~~~
/usr/include/boost/asio/basic_datagram_socket.hpp:69:12: note:   candidate expects 1 argument, 0 provided
app/CMakeFiles/network-test-tool-lib.dir/build.make:110: recipe for target 'app/CMakeFiles/network-test-tool-lib.dir/src/ntt/NetworkTestToolServer.cpp.o' failed
make[2]: *** [app/CMakeFiles/network-test-tool-lib.dir/src/ntt/NetworkTestToolServer.cpp.o] Error 1
CMakeFiles/Makefile2:1228: recipe for target 'app/CMakeFiles/network-test-tool-lib.dir/all' failed
make[1]: *** [app/CMakeFiles/network-test-tool-lib.dir/all] Error 2
Makefile:140: recipe for target 'all' failed
make: *** [all] Error 2
user2@user2-VirtualBox:~/network_test_tool/network-test-tool/build$ 

1 个答案:

答案 0 :(得分:0)

  

https://www.boost.org/doc/libs/1_67_0/doc/html/boost_asio/reference/basic_datagram_socket/basic_datagram_socket.html

udp::socket没有默认构造函数。你的构造函数是,

NetworkTestToolServer::NetworkTestToolServer(short port)
{
    my_io_service = new boost::asio::io_service();

    server_endpoint = boost::asio::ip::udp::endpoint(boost::asio::ip::udp::v4(),
                                                     50002);

    server_udp_socket = boost::asio::ip::udp::socket(*my_io_service, 
                                                     server_endpoint);
}

这是编写构造函数的一种非常糟糕的方式。构造函数将为类中的每个成员调用默认构造函数。在您的情况下,udp::socket没有默认构造函数。这就是它无法编译的原因。只有在默认构造之后,才会执行大括号块中的语句。你需要的是member initializer list。有关更多参考,请参阅此stackoverflow question

你想要的是这样的,

class NetworkTestToolServer
{
    using udp = boost::asio::ip::udp;
    private:
        boost::asio::io_service*       my_io_service;
        udp::endpoint server_endpoint;
        udp::socket   server_udp_socket;
        udp::endpoint sender_endpoint;
        udp::endpoint client_endpoint;
    public:
        NetworkTestToolServer(short port);
};

NetworkTestToolServer::NetworkTestToolServer(short port)
  : my_io_service(new boost::asio::io_service),
    server_endpoint(udp::endpoint(udp::v4(), port)),
    server_udp_socket(udp::socket(*my_io_service, server_endpoint))
{
}

不幸的是,gcc没有产生非常好的错误代码。 clang给出了更好的错误,

udp_bad.cpp:15:24: error: constructor for 'NetworkTestToolServer' must explicitly initialize the member 'server_udp_socket' which does not have a default constructor
NetworkTestToolServer::NetworkTestToolServer(short port)

下次尝试多个编译器。