使用已删除的功能错误

San*_*Kim 3 c++ c++11

我遇到过/usr/include/c++/4.6/ext/new_allocator.h:108:9: error: use of deleted function ‘SMIBQueue::SMIBQueue(const SMIBQueue&)’C++ eclipse 的问题.

我正在使用-std=c++0x标志来使用C++ 11

虽然我发现了错误发生的重点,但我不知道为什么.

这是头文件

class SMIBQueue{
private:
    std::queue<unsigned char> exceptionQueue;
    std::queue<UserRequest*> userInputQueue;
    std::queue<LpRsp*> lpRspQueue;
    std::queue<LpCmd*> lpCmdQueue;

    std::mutex EvtQueueLock;
    std::mutex UserQueueLock;
    std::mutex LpRspQueueLock;
    std::mutex LpCmdQueueLock;
public:

    int evtGetItem(unsigned char &item);
    int evtPutItem(unsigned char item);
    int userGetItem(UserRequest*& item);
    int userPutItem(UserRequest* item);
    int lpGetItem(LpCmd*& queue_buf);
    int lpPutItem(LpCmd *queue_buf);
    int lpRspGetItem(LpRsp*& queue_buf);
    int lpRspPutItem(LpRsp *queue_buf);
    int lpRemoveQueuedInfo();
    int lpRspRemoveQueuedInfo();
};

class SMIBQueues{
public:
    static std::vector<SMIBQueue> smibQueues;
    static void queueInit(int numOfClient);
    static int evtGetItem(int sasAddr, unsigned char &item);
    static int evtPutItem(int sasAddr, unsigned char item);
    static int userGetItem(int sasAddr, UserRequest*& item);
    static int userPutItem(int sasAddr, UserRequest* item);
    static int lpGetItem(int sasAddr, LpCmd*& queue_buf);
    static int lpPutItem(int sasAddr, LpCmd *queue_buf);
    static int lpRspGetItem(int sasAddr, LpRsp*& queue_buf);
    static int lpRspPutItem(int sasAddr, LpRsp *queue_buf);
    static int lpRemoveQueuedInfo(int sasAddr);
    static int lpRspRemoveQueuedInfo(int sasAddr);
};
Run Code Online (Sandbox Code Playgroud)

这是错误发生的功能

std::vector<SMIBQueue> SMIBQueues::smibQueues;

void SMIBQueues::queueInit(int numOfClient){
    for(int i = 0 ; i < numOfClient; i++){
        SMIBQueue s;
        smibQueues.push_back(s);
    }
}
Run Code Online (Sandbox Code Playgroud)

在这个函数中,push_pack方法产生错误.当我消除部件时,编译时没有问题.

Fil*_*efp 6

std::mutex有一个已删除的复制构造函数,这意味着SMIBQueue将隐式删除其复制构造函数,因为编译器无法为您生成一个.

如何复制具有无法复制成员的类?您将需要显式定义自己的拷贝构造函数,如果你想SMIBQueue成为可复制.


我的代码在哪里需要复制?

当你调用smibQueues.push_back (s)的副本s将被存储在里面smibQueues,这需要(隐式删除)拷贝构造函数.

你可以避开这个特定的使用问题smibQueues.emplace_back (),以默认,构建一个SMIBQueue内部的直接smibQueues(即std::vector).

注意:记住,在其他操作期间smibQueues可能需要SMBIQueue可复制/移动,例如,如果它尝试调整底层缓冲区的大小等.


推荐的解决方案

如果您计划在需要复制的上下文中使用它,请定义您自己的复制构造函数,但请记住std::mutex,根据标准,该复制构造函数既不可复制也不可移动.

  • 即使使用 `emplace_back()`,问题仍然存在。向量仍然需要能够在调整大小期间复制或移动它们的对象(显然,在 C++11 中移动是首选)。然而,再一次,`std::mutex` 被要求*两者都不是*,事实上,标准要求正好相反。C+11 30.4.1.2 [thread.mutex.requirements.mutex] 明确规定“互斥类型不得复制或移动”。使用 OP 的 class def as-presented 为通用的 `emplace_back()` 编写代码,但它仍然无法工作。 (2认同)