我有以下錯誤:ASIO試圖引用刪除功能
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();
我知道這是因爲其中一名私人成員無法複製,但我無法弄清楚如何解決它!我不想實現複製構造函數。
它看起來像'sender_thread'可能試圖創建's'的副本,但複製構造函數被刪除,它會失敗。 – callyalater
@ M.M沒有'sender_thread'構造函數,它是std的lib線程! – moritzg
@callyalater我該如何解決這個問題?我是否需要提供自己的拷貝構造函數? – moritzg