我在嘗試創建使用C++ 11標準的VC++靜態庫時遇到問題線程。錯誤C2280:'std :: thread :: thread(const std :: thread&)':嘗試引用已刪除的函數
我目前有兩個類,我能夠聲明和稍後定義一個線程就好了我的起始類(這是宣佈最後)。在這個階段,代碼只是一個套接字偵聽器,然後創建另一個類的對象來處理接受的每個客戶端。這些子對象應該創建並行數據捕獲,編碼和傳輸所需的線程。
的問題是:如果我宣佈一個std ::線程我的其他類,即使完全像我做我的啓動類,不管是什麼,我得到關於構建這個錯誤error C2280: 'std::thread::thread(const std::thread &)' : attempting to reference a deleted function [...]\vc\include\functional 1124 1
的唯一的辦法,我能夠解決這個錯誤是根本沒有聲明std::thread
對象在後一類,這是不可能的,根據我想要它做什麼...
我使用VS2013,並且我的來源是:
stdafx.h
#pragma once
#include "targetver.h"
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
#include <Windows.h>
#include <WinSock2.h>
#include <WS2tcpip.h>
#include <thread>
#include <iostream>
#include <vector>
StreamServer.h
#pragma once
#define DEFAULT_BUFLEN 65535
#define DEFAULT_PORT "5649"
class StreamServerClient
{
public:
bool* terminate;
//std::thread client; //If I comment this line out, it builds just fine.
void DoNothing();
StreamServerClient(SOCKET clientSock, bool* ptTerm);
StreamServerClient();
~StreamServerClient();
};
class StreamServer
{
public:
bool terminate;
std::thread Listener;
std::vector<StreamServerClient> clients;
void CreateClient(SOCKET, bool*);
void Listen();
StreamServer();
~StreamServer();
};
StreamServer.cpp
#include "stdafx.h"
#include "StreamServer.h"
StreamServerClient::StreamServerClient(SOCKET clientSock, bool* ptTerm)
{
terminate = ptTerm;
//client = std::thread(&StreamServerClient::DoNothing, this); //Same thing as the declaration
}
StreamServerClient::StreamServerClient()
{
*terminate = false;
//client = std::thread(&StreamServerClient::DoNothing, this); //Same thing as the declaration
}
void StreamServerClient::DoNothing()
{
}
StreamServerClient::~StreamServerClient()
{
}
void StreamServer::Listen()
{
{...}
do {
clients.push_back(StreamServerClient::StreamServerClient(accept(listenSock, NULL, NULL), &terminate));
std::cout << "accepted a client!" << std::endl;
} while (!terminate);
}
StreamServer::StreamServer()
{
terminate = false;
Listener = std::thread(&StreamServer::Listen, this);
Listener.detach();
}
StreamServer::~StreamServer()
{
}
'std :: thread's不能被複制。又名,複製構造函數被「刪除」。 – 2013-12-21 21:09:56
@remyabel但是爲什麼我能夠爲StreamServer做到這一點呢?我在這裏錯過了什麼? – Mismatch
@remyabel廢話,我想我只是注意到會發生什麼......如果我註釋掉'clients.push_back(...)',它也會消失。我應該使用'std :: thread *'來代替嗎? – Mismatch