I think the problem is your GameObject::setTexture method. You are passing the sf::Texture by value which means that particular instance will be destroyed as soon as that method returns. You need to pass a reference or pointer and keep that reference or pointer alive somewhere else.
So a quick example:
In GameObject.h
// Rest of Game Object Declaration
void setTexture(const sf::Texture & gameobject_texture);
// Rest of Game Object Declaration
In GameObject.cpp
// Rest of Game Object Definition
// Basically the same as before
void GameObject::setTexture(const sf::Texture & gameobject_texture) {
sprite.setTexture(gameobject_texture);
}
// Rest of Game Object Definition
In file where you set the texture:
// Rest of Code
sf::Texture texture; // IMPORTANT: Make sure texture outlives the GameObject
// Initialize the texture somehow
GameObject::GameObject* gameobject = GameObject::getGameObject("player_gameobject");
gameobject->setTexture(texture);
// Rest of Code
-- OR --
If you have C++11 or better compatibility (At least I think this should work)
// Rest of Code
std::shared_ptr<sf::Texture> texture = std::make_shared(new Texture()); // Now you can store the pointer somewhere else
// Initialize the texture somehow
GameObject::GameObject* gameobject = GameObject::getGameObject("player_gameobject");
gameobject->setTexture(*texture);
// Rest of Code
You could also use
std::unique_ptr<sf::Texture>
which enforces stricter ownership rules.