I can't add a smart pointer to the vector
There is a class MyClass, of course, with a constructor, you need to create a vector of smart pointers to objects of this class. The pointer itself is created, but an error occurs when trying to add it to the vector . What did I miss?
#include <memory>
#include <vector>
using namespace std;
Int main()
{
vector<unique_ptr<MyClass>> vectorPtr;
unique_ptr<MyClass> p1(new MyClass);
// до этого момента всё в порядке
vectorPtr.push_back(p1);
return 0;
}
4
1 answers
std::unique_ptr
it doesn't have a copy constructor, so to put it in a vector, you need to move it there:
vectorPtr.push_back(std::move(p1));
Or so:
vectorPtr.push_back(std::make_unique<MyClass>())
Or create it directly in the vector:
vectorPtr.emplace_back(new MyClass);
7
Author: ixSci, 2017-08-01 09:53:01