I have a test application for a assignment where I have to push_back a shared pointer into a vector of shared pointers. However it is crashing on the push_back and I am not sure why.
I tried moving the shared_ptr instead of copying it to the vector and am still seeing the same issue. I tried to debug the issue, but for some reason the variables are not showing up in the debugger after I click on them (possible IDE issue). I did see a similar issue on stack overflow, but this is not the same issue as the shared pointer is being initialized before the push_back is called.
//Fill - will loop through and ask the user what elements to add
void fill(std::vector<std::shared_ptr<Test>> &vec, int num)
{
for(int i {1}; i <= num; i++)
{
std::cout << "Enter data pointer [" << i << "] : ";
int data {};
std::cin >> data;
std::shared_ptr<Test> data_ptr = std::make_shared<Test> (data);
std::cout << "Created shared pointer for " << data_ptr->get_data() <<
std::endl; //debug
vec.push_back(std::move(data_ptr));
std::cout << "Added to vector" << data_ptr->get_data() << std::endl;
//debug (crash issue is with the pushback method)
}
}
The program is crashing when after the shared_ptr is created and the push_back method doesn't appear to execute. I added cout debug messages to try and see where in this function the program is failing. the "created shared ptr" is called, but the "Added to vector" is not called.
Test
? Also, what isget_data()
? I don't see it as part of thestd::shared_ptr
API... – BobMoranestd::cout << "Created shared pointer for " << data_ptr->get_data() << std::endl;
– BobMorane