我有以下错误:
Error C2280 'Sender::Sender(const Sender &)': attempting to reference a deleted function
这是我的代码:
#pragma once
#include <iostream>
#include <string>
#include "asio.hpp"
using namespace asio;
class Sender
{
public:
Sender(io_service& io_service,
const std::string& host,
const std::string& port) :
io_service_(io_service),
socket_(io_service, ip::udp::endpoint(ip::udp::v4(), 0))
{
ip::udp::resolver resolver(io_service_);
ip::udp::resolver::query query(ip::udp::v4(), host, port);
ip::udp::resolver::iterator iter = resolver.resolve(query);
endpoint_ = *iter;
};
~Sender()
{
socket_.close();
};
void operator()()
{
std::string msg = "";
for (;;)
{
std::cout << "Message: ";
std::cin >> msg;
std::cout << "The value you entered is " << msg << std::endl;
}
}
private:
io_service& io_service_;
ip::udp::socket socket_;
ip::udp::endpoint endpoint_;
};
我这样称呼它:
io_service io_service;
Sender s{ io_service, "localhost", "1337" };
std::thread sender_thread{ s };
sender_thread.join();
我知道这是因为其中一个私人成员无法复制,但我无法弄清楚如何修复它!我不想实现复制构造函数。
答案 0 :(得分:2)
Sender
是可移动但不可复制的,因为boost::asio
的套接字是可移动的但不可复制(参见文档)。
您需要std::move()
您的发件人进入线程的构造函数。