使用已删除的函数出错

use of deleted function error

本文关键字:函数 出错 删除      更新时间:2023-10-16

我有一个问题,/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);
};

,这是错误发生在

的函数
std::vector<SMIBQueue> SMIBQueues::smibQueues;
void SMIBQueues::queueInit(int numOfClient){
    for(int i = 0 ; i < numOfClient; i++){
        SMIBQueue s;
        smibQueues.push_back(s);
    }
}

在这个函数中,push_pack方法产生错误。当我删除这部分时,编译时就没有问题了。

std::mutex已经删除了复制构造函数,这意味着SMIBQueue将隐式删除其复制构造函数,因为编译器不能为您生成一个。

如果一个类的成员不能被复制,它将如何去复制呢?如果您希望SMIBQueue 可复制,则需要显式地定义自己的复制构造函数


我的代码需要在哪里做一个副本?

调用smibQueues.push_back (s)时,s的副本将存储在smibQueues中,这需要(隐式删除)复制构造函数

您可以通过使用smibQueues.emplace_back ()默认构造直接在smibQueues中构建SMIBQueue来绕过这个特定的问题。std::vector)。

注意:请记住,smibQueues可能要求SMBIQueue在其他操作期间是可复制/可移动的,例如当它试图调整底层缓冲区的大小时,等等。


推荐解决方案

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