Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ error: use of deleted function boost::asio::io_context::io_context

I'm having this error when trying to instantiate a new object of a class. The code is:

using boost::asio::ip::tcp;
typedef boost::asio::io_service ioservice;

class cnx
{
public:
    cnx(ioservice io);

private:
    tcp::socket *s;
};

//constructor:
cnx::cnx(ioservice io)
{
    this->s = new tcp::socket(io);
}

outside the cpp/h file of cnx, I try to instantiate as:

ioservice io;
cnx c(io);

or

cnx* c = new cnx(io);

and both result in this error message. What can be causing this?

like image 722
Fnr Avatar asked Nov 29 '25 15:11

Fnr


1 Answers

As @tkausl said in the comments and also thanks to this answer, the problem was because boost::asio::io_service is not copyable. Changing the definition of the constructor to:

cnx(ioservice& io);

and calling it through:

ioservice io;
cnx c(std::ref(io));

solved the problem

like image 98
Fnr Avatar answered Dec 02 '25 04:12

Fnr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!