错误C2280:'std :: thread :: thread(const std :: thread&)':尝试引用一个删除的函数 [英] Error C2280: 'std::thread::thread(const std::thread &)' : attempting to reference a deleted function

查看:4523
本文介绍了错误C2280:'std :: thread :: thread(const std :: thread&)':尝试引用一个删除的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试创建使用C ++ 11标准线程的VC ++静态库时遇到问题。

I'm having a problem trying to create a VC++ Static Library that uses C++11 standard threads.

我目前有两个类,我可以声明和以后定义一个线程只是在我的开始类(这是最后声明)。在这个阶段,代码只是一个套接字监听器,然后创建另一个类的对象来处理每个客户端接受。这些子对象应该创建我需要并行数据捕获,编码和传输的线程。

I currently have two classes, and I am able to declare and later define a thread just fine on my starting class (which is declared last). At this stage, the code is just a socket listener which then creates an object of another class to handle each client accepted. Those child objects are supposed to create the threads I need for parallel data capture, encoding and transmission.

问题是:如果我在我的其他人声明一个std ::线程类,即使完全像我在我的开始类,不管什么,我得到这个错误在构建错误C2280:'std :: thread :: thread(const std :: thread& )':尝试引用已删除的函数[...] \vc\include\functional 1124 1

The problem is: If I declare a std::thread on my other class, even if exactly like I did on my start class, no matter what, I'm getting this error on build error C2280: 'std::thread::thread(const std::thread &)' : attempting to reference a deleted function [...]\vc\include\functional 1124 1

我能解决这个错误是简单地不在后面的类中声明一个 std :: thread 对象,这是不可能的,根据我想要它做...

The only way I was able to workaround this error is to simply not declare a std::thread object in the latter class, which isn't possible, according to what I want it to do...

我正在使用VS2013,我的来源是:

I'm using VS2013, and my sources are:

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 类型的对象。您最好只是初始化成员初始化器列表中的对象:

Objects of type std::thread can't be copied. You are best off to just initialize the objects in the member initializer list:

class StreamServerClient
{
public:
    bool* terminate;
    std::thread client;
    void DoNothing();
    StreamServerClient(SOCKET clientSock, bool* ptTerm);
    StreamServerClient(StreamServerClient&& other);
    ~StreamServerClient();
};

StreamServerClient::StreamServerClient(SOCKET clientSock, bool* ptTerm)
    : terminate(ptTerm)
    , client(std::thread(&StreamServerClient::DoNothing, this)) {
}
StreamServerClient::StreamServerClient(StreamServerClient&& other)
    : terminate(other.terminate)
    , client(std::move(other.client)) {
}

我忽略了默认的构造函数它尝试为解除引用未初始化的指针的结果分配一个值),而是添加了一个移动构造函数:当返回到 std :: vector< ...> 当提供看起来像临时的东西(即,是临时的或者看起来像一个,例如使用 std :: move ())。

I ommitted the default constructor (note that your version doesn't work because it tries to assign a value to the result of dereferencing an uninitialized pointer) and instead added a move constructor: when pushing back to the std::vector<...> this constructor will be called when providing something which looks like a temporary (i.e., something which either is a temporary or is made to look like one, e.g., using std::move()).

这篇关于错误C2280:'std :: thread :: thread(const std :: thread&amp;)':尝试引用一个删除的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆