4

i have had a problem with /usr/include/c++/4.6/ext/new_allocator.h:108:9: error: use of deleted function ‘SMIBQueue::SMIBQueue(const SMIBQueue&)’ with C++ eclipse.

and i am using -std=c++0x flag to use C++11

though i found the point that the error occur at, i don't know why.

this is header file

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

and this is the function that the error occur at

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

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

in this function, push_pack method makes the error. when i eliminate the part, there is no problem while compiling.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
SangminKim
  • 8,358
  • 14
  • 69
  • 125

1 Answers1

7

std::mutex has a deleted copy-constructor, this means that SMIBQueue will have its copy-constructor implicitly deleted since the compiler cannot generate one for you.

How would it go about copying a class which has members that cannot be copied? You will explicitly need to define your own copy-constructor if you want SMIBQueue to be copyable.


WHERE DOES MY CODE REQUIRE A COPY TO BE MADE?

When you are calling smibQueues.push_back (s) a copy of s will be stored inside smibQueues, which requires (the implicitly deleted) copy-constructor.

You can circumvent this specific problem by using smibQueues.emplace_back () to default-construct a SMIBQueue directly inside your smibQueues (ie. std::vector).

Note: Just keeo in mind that smibQueues might require SMBIQueue to be copyable/movable during other operations, such as if it tries to resize the underlying buffer, etc.


RECOMMENDED SOLUTION

Define your own copy-constructor if you plan to use it in contexts which require copying, but do keep in mind that std::mutex is neither copyable or movable, according to the Standard.

Filip Roséen - refp
  • 62,493
  • 20
  • 150
  • 196
  • Or just call `smibQueues.resize(smibQueues.size() + numOfClient)` to implicitly construct all the extra entries. – kennytm May 30 '14 at 10:30
  • 2
    Even with `emplace_back()` the problem will persist. Vectors still need to be able to copy or move their objects during resizing (moving being preferred in C++11, obviously). Once again, however, `std::mutex` is required to be *neither*, and in fact mandated by the standard to be just the opposite. C+11 30.4.1.2 [thread.mutex.requirements.mutex] dictates specifically that "The mutex types shall not be copyable or movable." Punch up the code for the generic `emplace_back()` with the OP's class def as-presented and it still won't work. – WhozCraig May 30 '14 at 10:34
  • @WhozCraig sure, doing a resize the elements must be *movable/copyable*, I was directly addressing the problem with the line in question. I'll edit my answer to reflect this. – Filip Roséen - refp May 30 '14 at 10:42
  • 1
    @FilipRoséen-refp Sure. Btw, `smibQueues.emplace_back()` (empty parens important) *will* work with a `std::deque<>`. – WhozCraig May 30 '14 at 10:45