错误C2280:'std :: thread :: thread(const std :: thread&)':尝试引用已删除的函数

不匹配

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

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

问题是:如果我在另一个类上声明了一个std :: thread,即使与我在开始类上所做的完全一样,无论如何,我在构建时会遇到此错误 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()
{
}
迪特玛·库尔(DietmarKühl)

类型的对象std::thread无法复制。最好只初始化成员初始化器列表中的对象:

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)) {
}

我省略了默认构造函数(请注意,您的版本无法正常工作,因为它试图为取消引用未初始化的指针的结果分配一个值),而是添加了move构造函数:当推回std::vector<...>this构造函数时,会在提供某些内容时调用它看起来像是临时的(即,某些东西临时的,或者看起来像是一个临时的东西,例如使用std::move())。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

错误:使用已删除的功能'std :: thread :: thread(const std :: thread&)'

来自分类Dev

C ++ std :: thread“尝试使用已删除的函数”

来自分类Dev

C ++ 11 std :: thread std :: move尝试使用已删除的函数

来自分类Dev

显示 std::thread 函数传递的错误数据。

来自分类Dev

如何修复错误'std :: promise <int> :: promise(const std :: promise <int>&)':尝试引用已删除的函数

来自分类Dev

如何修复错误'std :: promise <int> :: promise(const std :: promise <int>&)':尝试引用已删除的函数

来自分类Dev

std :: thread类与c ++中的std :: this_thread命名空间?

来自分类Dev

std :: vector的std :: thread后代

来自分类Dev

如何终止std :: thread?

来自分类Dev

如何使用std :: thread?

来自分类Dev

iOS上的std :: thread

来自分类Dev

如何终止std :: thread?

来自分类Dev

调用加入后删除std :: thread吗?

来自分类Dev

没有参数的std :: thread构造函数

来自分类Dev

暂停std :: thread直到函数完成

来自分类Dev

std :: thread ---没有匹配的构造函数

来自分类Dev

std :: thread Args ...列表中的函数指针

来自分类Dev

std :: thread Args ...列表中的函数指针

来自分类Dev

std :: thread构造函数(变量数量)

来自分类Dev

可连接std :: thread的析构函数

来自分类Dev

用不同的参数函数运行std :: thread

来自分类Dev

在函数方法中使用std :: thread

来自分类Dev

std :: thread :: id的std :: operator ==中的分段错误

来自分类Dev

c ++ 11 std :: thread与类友函数之间的交互

来自分类Dev

C ++ 11使用std :: thread运行模板化类函数

来自分类Dev

C ++ 11使用std :: thread运行模板化类函数

来自分类Dev

C ++ 11:具有std :: thread和lambda函数的Segfault

来自分类Dev

C ++ std :: thread和方法类

来自分类Dev

C ++-执行时std :: thread崩溃

Related 相关文章

  1. 1

    错误:使用已删除的功能'std :: thread :: thread(const std :: thread&)'

  2. 2

    C ++ std :: thread“尝试使用已删除的函数”

  3. 3

    C ++ 11 std :: thread std :: move尝试使用已删除的函数

  4. 4

    显示 std::thread 函数传递的错误数据。

  5. 5

    如何修复错误'std :: promise <int> :: promise(const std :: promise <int>&)':尝试引用已删除的函数

  6. 6

    如何修复错误'std :: promise <int> :: promise(const std :: promise <int>&)':尝试引用已删除的函数

  7. 7

    std :: thread类与c ++中的std :: this_thread命名空间?

  8. 8

    std :: vector的std :: thread后代

  9. 9

    如何终止std :: thread?

  10. 10

    如何使用std :: thread?

  11. 11

    iOS上的std :: thread

  12. 12

    如何终止std :: thread?

  13. 13

    调用加入后删除std :: thread吗?

  14. 14

    没有参数的std :: thread构造函数

  15. 15

    暂停std :: thread直到函数完成

  16. 16

    std :: thread ---没有匹配的构造函数

  17. 17

    std :: thread Args ...列表中的函数指针

  18. 18

    std :: thread Args ...列表中的函数指针

  19. 19

    std :: thread构造函数(变量数量)

  20. 20

    可连接std :: thread的析构函数

  21. 21

    用不同的参数函数运行std :: thread

  22. 22

    在函数方法中使用std :: thread

  23. 23

    std :: thread :: id的std :: operator ==中的分段错误

  24. 24

    c ++ 11 std :: thread与类友函数之间的交互

  25. 25

    C ++ 11使用std :: thread运行模板化类函数

  26. 26

    C ++ 11使用std :: thread运行模板化类函数

  27. 27

    C ++ 11:具有std :: thread和lambda函数的Segfault

  28. 28

    C ++ std :: thread和方法类

  29. 29

    C ++-执行时std :: thread崩溃

热门标签

归档