I've created a Vector of type <sf::Drawable*>. I've just started playing around with SFML so I'm still learning the ropes.
Anyway, the Vector is stored in it's own class (think of it as a sprite [or drawable] manager class). I'm creating the Sprite objects in main and then adding them to the Drawable class.
When I try to access the files in the vector, however, I get segfaults when I try and draw them. I think it may be because the Sprites are going out of scope. This is the actual code I'm using to create the sprites:
sf::Sprite sprite;
for (int i = 0; i < size; i++)
{
sprite = sf::Sprite();
sprite.SetImage(images[rand() % 3]); //Choosing between 1 of 3 Image objects
sprite.SetPosition(rand() % 800, rand() % 600);
layer.addToLayer(dynamic_cast<sf::Drawable*>(&sprite));
}
If my understanding is correct, the line sprite = sf::Sprite() is creating the object on the stack, not the heap, which is the root of my problems.
I've tried to remedy this by sprite = new sf::Sprite() but I get the error:
main.cpp:108: error: no match for ‘operator=’ in ‘sprite = (((sf::Sprite*)operator new(196u)), (<statement>, <anonymous>))’
/usr/include/SFML/Graphics/Sprite.hpp:45: note: candidates are: sf::Sprite& sf::Sprite::operator=(const sf::Sprite&)
Any ideas?