无法编译boost :: asio :: basic_datagram_socket <boost :: asio :: ip :: udp> :: basic_datagram_socket()

时间:2018-01-16 15:13:13

标签: c++ boost boost-asio

我试图创建一个合适的udpServer类,它将成为我的udpServerBoost的父级。 (udpServer没有实现atm)

因为我试图改变我的类构造函数让它自己创建io_service我在编译我的src时出错了

src/UdpServerBoost.cpp: In constructor ‘UdpServerBoost::UdpServerBoost()’:
src/UdpServerBoost.cpp:44:32: error: no matching function for call to ‘boost::asio::basic_datagram_socket<boost::asio::ip::udp>::basic_datagram_socket()’
 UdpServerBoost::UdpServerBoost()
                                ^
In file included from /usr/include/boost/asio.hpp:21:0,
                 from src/UdpServerBoost.hpp:9,
                 from src/UdpServerBoost.cpp:1:
/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<_U1, _T1>::value>::type*)
   basic_datagram_socket(
   ^
/usr/include/boost/asio/basic_datagram_socket.hpp:183:3: note:   template argument deduction/substitution failed:
src/UdpServerBoost.cpp:44:32: note:   candidate expects 2 arguments, 0 provided
 UdpServerBoost::UdpServerBoost()
                                ^
In file included from /usr/include/boost/asio.hpp:21:0,
                 from src/UdpServerBoost.hpp:9,
                 from src/UdpServerBoost.cpp:1:
/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
src/UdpServerBoost.cpp:47:70: error: no matching function for call to ‘boost::asio::basic_datagram_socket<boost::asio::ip::udp>::basic_datagram_socket(boost::asio::io_service*, boost::asio::ip::udp::endpoint)’
     socket_ = udp::socket(&io_service, udp::endpoint(udp::v4(), 1300));
                                                                      ^
In file included from /usr/include/boost/asio.hpp:21:0,
                 from src/UdpServerBoost.hpp:9,
                 from src/UdpServerBoost.cpp:1:
/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<_U1, _T1>::value>::type*)
   basic_datagram_socket(
   ^
/usr/include/boost/asio/basic_datagram_socket.hpp:183:3: note:   template argument deduction/substitution failed:
src/UdpServerBoost.cpp:47:70: note:   mismatched types ‘boost::asio::basic_datagram_socket<Protocol1, DatagramSocketService1>’ and ‘boost::asio::io_service*’
     socket_ = udp::socket(&io_service, udp::endpoint(udp::v4(), 1300));
                                                                      ^
In file included from /usr/include/boost/asio.hpp:21:0,
                 from src/UdpServerBoost.hpp:9,
                 from src/UdpServerBoost.cpp:1:
/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, 2 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, 2 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:   no known conversion for argument 1 from ‘boost::asio::io_service*’ to ‘boost::asio::io_service&’
/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:   no known conversion for argument 1 from ‘boost::asio::io_service*’ to ‘boost::asio::io_service&’
/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, 2 provided

我已经使用g ++编译了boost库,但它确实有效。所以我知道问题并非来自它:(

所以这是我的来源!感谢阅读^^

度过愉快的一天

这是我的 udpServerBoost.cpp

UdpServerBoost::UdpServerBoost()
{
    boost::asio::io_service io_service;
    socket_ = udp::socket(&io_service, udp::endpoint(udp::v4(), 1300));

    memset(&recv_buffer_, 0, 2048);
    clientList_ = std::vector<ClientBoost>();
    startReceive();
    io_service.run();
}

.hpp

class UdpServerBoost
{
  public:
    UdpServerBoost();
    ~UdpServerBoost() {};

  private:
    void startReceive();

    void handleReceive(const boost::system::error_code &error,
                       std::size_t bytes_transferred);

    void handleSend(boost::shared_ptr<std::string> /*message*/,
                    const boost::system::error_code & /*error*/,
                    std::size_t /*bytes_transferred*/);

    ClientBoost getClient();

    void treatMessage(std::string mess);
    void sendToClient(ClientBoost client, std::string mess);

    std::vector<ClientBoost> clientList_;
    udp::socket socket_;
    udp::endpoint remote_endpoint_;
    boost::array<char, 2048> recv_buffer_;
};

1 个答案:

答案 0 :(得分:2)

你是默认构造套接字。这是不允许的。您必须至少提供对其执行者(io_service)的引用。

尝试这样的事情:

class UdpServerBoost
{
  UdpServerBoost();

  boost::asio::io_service my_io_service_;
  udp::socket my_socket_;
};

UdpServerBoost::UdpServerBoost()
: my_io_service_()
, my_socket_(my_io_service_)
{
}