I am somewhat new to using unique_ptr and was using it in some of my code. While trying to use unique_ptr, I have the following code which doesn't seem to work, could anyone explain me why?
#include <vector>
#include <deque>
#include <memory>
class MyClass {
public:
MyClass(int x) {
myDeque.push_back(std::unique_ptr<int>(new int(x)));
}
private:
std::deque<std::unique_ptr<int>> myDeque;
};
int main() {
std::vector<MyClass> myVector;
myVector.push_back(MyClass(5));
}
I wish to have a deque of unique_ptr in a class and would populate it from another function, and I also need to have a vector of class objects of MyClass. The code works when I make myDeque global but throws error when it is a member variable.