The problem is in your addItem
method:
void Player::addItem(Object newItem)
{
inventory.push_back(&newItem);
return;
}
Here, you're passing newItem
by value. This means that newItem
, as a function parameter, is effectively a local variable, holding a copy of the object passed to it. You then take a pointer to this local variable and store it for later use, but it goes out of scope at the end of the function. At this point the pointer is no longer valid.
To fix this, you should pass newItem
by reference: preferably as an actual reference (Object&
) but passing a pointer to it would work too.