C++ Error: use of deleted function during constructor call of custom class

3.5k views Asked by At

I have a custom class producer which inherits from SystemC class sc_module:

class producer: public sc_module {
    public:
        int counter;
        sc_in<bool> clock;
        sc_out<msg> out;
        int speed;

        producer(sc_module_name name, int speed) : 
            sc_module(name), 
            speed(speed) 
        {
            SC_HAS_PROCESS(producer);
            SC_METHOD(produce);

            sensitive << clock.pos();
            counter = 0;
        }

        void produce() {
            ...
        }

};

Later in the SystemC sc_main-class I want to put a bunch of objects in a std::vector<producer>:

std::vector<producer> producers;
for(int i = 0; i < numIn; i++){
    producers.at(i) = producer("Producer " + i, genSpeed); // <- Here the error occurs
}

And here is the compiler error:

error: use of deleted function ‘producer& producer::operator=(producer&&)’
         producers.at(i) = producer("Producer " + i, genSpeed);
                                                             ^
dist.cpp:103:7: note: ‘producer& producer::operator=(producer&&)’ is implicitly deleted because the default definition would be ill-formed:
 class producer: public sc_module {
       ^~~~~~~~

Why does the error occur? How can I fix it?

3

There are 3 answers

0
goulashsoup On BEST ANSWER

I our final solution we used std::shared_ptr and std::vector:

using sharedProducerPointer = std::shared_ptr<producer>;
std::vector<sharedProducerPointer> producers;

//bind producers and distributor to input signals
for(int i = 0; i < numIn; i++) {
    std::string stringProducersName = "producer_" + std::to_string(i);
    const char* charProducersName = stringProducersName.c_str();

    sharedProducerPointer prod = sharedProducerPointer(new producer(charProducersName, genSpeed, numOut));
    prod->out.bind(inputSignals.at(i));
    dist.inputMsg.at(i).bind(inputSignals.at(i));
    prod->clock(inClock);

    producers.push_back(prod);
}
1
Martin Bonner supports Monica On

producer doesn't have a move assignment operator. By the looks of things, probably because sc_module doesn't have one.

You have other problems:

std::vector<producer> producers;
for(int i = 0; i < numIn; i++){
    producers.at(i) ....
}

This creates an empty vector of producers, and then assigns to the first element of that vector. The problem is that there is no such element, and because you are using at, you will get an exception.

Conveniently, you can fix both problems at once with:

std::vector<producer> producers;
for(int i = 0; i < numIn; i++){
    producers.emplace_back("Producer " + i, genSpeed);
}

This creates an empty vector, and then constructs a series of new producers at the end of it. This does depend on you having a working move constructor. If you don't have a working move constructor, I think you are rather stuck.

This still won't work, because you will get producers called "Producer ", "roducer ", "oducer ", etc. Adding an integer to a character string literal will just return a pointer to the n'th character of the literal. What you need is to make the literal be a std::string literal, and convert the integer to text.

    producers.emplace_back("Producer "s + std::to_string(i), genSpeed);
0
random On

Copy construction and assignment are disabled for sc_modules to implement/preserve SystemC elaboration semantics.

You should use sc_vector to create collections of sc_objects. It was created specifically to address this type of design problems.