5

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

error: cannot allocate an object of abstract type

for the line

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

where cells is a std::vector of the abstract type and cell is of type

std::unique_ptr<AbstractType>&& cell

(I actually pass cell to a handler class) I know that one can not instantiate an abstract type and as I'm understanding the std:move operator it need to instantiate the object somehow?

Can anybody help me how to manage the problem? Or should the function (not my part of the project) not return a unique pointer to an object of an abstract type?

Xeo
  • 129,499
  • 52
  • 291
  • 397
soriak
  • 672
  • 2
  • 13
  • 23

1 Answers1

9

You can't store AbstractType elements directly on a std::vector. You can simply store the unique_ptrs themselves in a std::vector<std::unique_ptr<AbstractType>> with cells.push_back(std::move(cell)).

R. Martinho Fernandes
  • 228,013
  • 71
  • 433
  • 510