lundi 28 décembre 2020

Error to implementation of strategy pattern in C++

I'm trying to implement the design pattern strategy in C++. The goal for me is to do an operation on 2 numbers.

Here is my context class

  operation::operation(std::unique_ptr<calculatorTask> pTask = nullptr)
{
    pCalculatorTask = std::move(pTask);
}

operation::~operation();
{
}

void operation::setTask(std::unique_ptr<calculatorTask> pTask)
{
    this->pCalculatorTask = std::move(pTask);
}

void operation::executeTask(numberMsg& sValues)
{
    pCalculatorTask->calculate(values);
}

This is my implementation :

int main()
{
   std::vector<std::unique_ptr<calculatorTask>> myOperation;
   myOperation.push_back(std::move(std::unique_ptr<additionTask>(new additionTask())));
   myOperation.push_back(std::move(std::unique_ptr<substractionTask>(new substractionTask())));
   for (const auto &Ope : myOperation)
    {
        pOpe->setTask(Ope);
        [...]
    }
}

I have this error :

error: use of deleted function ‘std::unique_ptr<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = calculatorTask; _Dp = std::default_delete]’ pOpe->setTask(Ope);

I don't understand error and how to fix it.

Aucun commentaire:

Enregistrer un commentaire