SFML community forums

Help => Graphics => Topic started by: danthebeliever on March 24, 2011, 09:28:25 pm

Title: [SOLVED] sf::Shape pointers
Post by: danthebeliever on March 24, 2011, 09:28:25 pm
Hi.

Code: [Select]
sf::Shape * genSquare =  new sf::Shape::Rectangle(0,0,100,100,sf::Color::Red);

This will not compile for me. I get an "expected type-specifier" error.

Any thoughts?

I need it to be a pointer else it will go out of scope for the other object I am using it for and thus cause a bad access.

EDIT: sf::Shape::Rectangle is not a constructor, it's a static function, so I
need to type it.

The correct code is this:
Code: [Select]
sf::Shape * genSquare =  new sf::Shape(sf::Shape::Rectangle(0,0,100,100,sf::Color::Red));
Title: [SOLVED] sf::Shape pointers
Post by: Nexus on March 24, 2011, 09:58:39 pm
sf::Shape::Rectangle is not a type, it is a static member function. Therefore you must create a sf::Shape dynamically and initialize it with the result of the Rectangle() function.
Title: [SOLVED] sf::Shape pointers
Post by: Laurent on March 24, 2011, 09:58:54 pm
sf::Shape::Rectangle is a static function, not a type name.
Code: [Select]
sf::Shape * genSquare = new sf::Shape(sf::Shape::Rectangle(0,0,100,100,sf::Color::Red));
Title: [SOLVED] sf::Shape pointers
Post by: danthebeliever on March 24, 2011, 10:04:23 pm
Great! That works! Thank you very much :)
Title: [SOLVED] sf::Shape pointers
Post by: Laurent on March 24, 2011, 10:07:19 pm
By the way...

Quote
I need it to be a pointer else it will go out of scope for the other object I am using it for and thus cause a bad access

You can simply store a copy of it, you probably don't need a pointer to a dynamically allocated instance.
Title: [SOLVED] sf::Shape pointers
Post by: danthebeliever on March 24, 2011, 10:10:57 pm
Oh gotcha thanks :)

Well I'm using cpGUI and it accepts a pointer for the cpShapeButton. If I pass the address of a statically allocated object it goes out of scope, and since it won't accept an object, I think I do have to allocate it.