SFML community forums

Help => General => Topic started by: er4zox on July 26, 2013, 07:56:32 pm

Title: RectangleShape in a vector
Post by: er4zox on July 26, 2013, 07:56:32 pm
Hello guys !

I have a vector inside a class:
std::vector<sf::RectangleShape*> pShapes;
sf::RectangleShape * p;

If I add something to the vector I do
p = new sf::RectangleShape;
pShapes.push_back(p);

Now the problem:
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const
        {
                for (int i = 0; i < pShapes.size(); ++i)
        {
                                target.draw(pShapes[i],states);
                }
        }

It gives me this error:
(http://picload.org/image/orppaic/sfmlvectorerror.png)

I searched for this problem but didn't find anything.

Thanks for your help!

Greetings
Er4zoX
Title: Re: RectangleShape in a vector
Post by: datMoka on July 26, 2013, 08:38:13 pm
Put a '*' in front of the 'pShapes' I believe.

You've declared it as a pointer but you're trying to draw it to the screen as if it were a normal variable.
Title: Re: RectangleShape in a vector
Post by: er4zox on July 26, 2013, 08:44:04 pm
Ah thank you!
Finally it works ;D

Greetings
Er4zoX
Title: Re: RectangleShape in a vector
Post by: eXpl0it3r on July 26, 2013, 09:35:11 pm
There's absolutely no need to use manual memory management here, especially since you most probably don't even delete the allocated memory and thus leave a memory leak mess behind...
Unlike in Java, the initialization in C++ doesn't require you to call new. Maybe you should go again over the "class" chapter in your C++ book. ;)