简体   繁体   中英

boost.asio test problem

I'm new on boost.asio. I have a problem when I try a simple example.

in my header file I have:

#include <boost/asio.hpp>
#include "boost/bind.hpp"
#include "boost/date_time/posix_time/posix_time_types.hpp"

and I need this private variable:

boost::asio::ip::udp::socket socket_;

I have this error at compile time:

error C2512: 'boost::asio::basic_datagram_socket<Protocol>' : no appropriate default constructor available
        with
        [
            Protocol=boost::asio::ip::udp

The list of all UDP socket constructors if found here . As you can see, you must provide at least a reference to a boost::asio::io_service object.

If this is a private variable, provide this reference in the class constructor's initializer list. The following will compile:

#include <boost/asio.hpp>
class Socket
{
    boost::asio::ip::udp::socket socket_;
 public:
    Socket( boost::asio::io_service& ioserv) : socket_(ioserv) {}
};
int main()
{
    boost::asio::io_service io;
    Socket s(io);
}

I have used boost::asio and I had a similar issue.

You need to make a constructor that takes a io_service object and initialize your socket_ with the io_service.

Like so:

tcp_connection::tcp_connection(boost::asio::io_service& io_service) : socket_(io_service) {}

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