I want to have a vector of pointers to objects in my class. To avoid making a destructor for it I wanted to use std::unique_ptr
, as the objects are created/owned/destructed in my class, but I have a compiler error I can't understand. Next code will serve as a short sample for my problem:
std::unique_ptr<int> createPtr(int value)
{
std::unique_ptr<int> ptr(new int(value));
return ptr;
};
int main()
{
std::vector<std::unique_ptr<int>> vec;
vec.push_back(createPtr(1));
std::unique_ptr<int> ptr = createPtr(2);
vec.push_back(ptr);//error C2248: 'std::unique_ptr<_Ty>::unique_ptr' : cannot access private member declared in class 'std::unique_ptr<_Ty>'
}
Can you please explain me why I get this error and what is the correct usage for std::unique_ptr
?