How to store a vector of abstract class objects that are set by std :: unique_ptr?

I have a loop in which I use a function that returns std :: unique_ptr to an abstract class object. I want to store these objects in std :: vector via push_back. But since objects are of an abstract type, I get the following error:

error: cannot allocate an object of abstract type

for line

  cells.push_back(std::move(*cell));

where the cells are of std::vectorabstract type, and cellhas type

std::unique_ptr<AbstractType>&& cell

(I actually pass cellto the handler class) I know that you cannot create an instance of an abstract type, and since I understand the std: move operator, it needs to somehow create an instance of the object

Can someone help me in solving this problem? Or should the function (and not my part of the project) not return a unique pointer to an object of an abstract type?

+5
1

AbstractType std::vector. unique_ptr std::vector<std::unique_ptr<AbstractType>> cells.push_back(std::move(cell)).

+9

All Articles