Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: RectangleShape in a vector  (Read 2435 times)

0 Members and 1 Guest are viewing this topic.

er4zox

  • Newbie
  • *
  • Posts: 7
    • View Profile
RectangleShape in a vector
« 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:


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

Thanks for your help!

Greetings
Er4zoX

datMoka

  • Newbie
  • *
  • Posts: 48
  • Wooo!
    • View Profile
Re: RectangleShape in a vector
« Reply #1 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.

er4zox

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: RectangleShape in a vector
« Reply #2 on: July 26, 2013, 08:44:04 pm »
Ah thank you!
Finally it works ;D

Greetings
Er4zoX

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11031
    • View Profile
    • development blog
    • Email
Re: RectangleShape in a vector
« Reply #3 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. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything