简体   繁体   English

C ++错误:使用已删除的函数boost :: asio :: io_context :: io_context

[英]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: 在cnx的cpp / h文件之外,我尝试实例化为:

ioservice io;
cnx c(io);

or 要么

cnx* c = new cnx(io);

and both result in this error message. 并且都导致此错误消息。 What can be causing this? 是什么原因造成的?

As @tkausl said in the comments and also thanks to this answer, the problem was because boost::asio::io_service is not copyable. 正如@tkausl在评论中所说,也要感谢这个答案,问题是因为boost::asio::io_service是不可复制的。 Changing the definition of the constructor to: 将构造函数的定义更改为:

cnx(ioservice& io);

and calling it through: 并通过以下方式调用:

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

solved the problem 解决了问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM