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

Author Topic: [SOLVED] sf::Shape pointers  (Read 4203 times)

0 Members and 1 Guest are viewing this topic.

danthebeliever

  • Newbie
  • *
  • Posts: 10
    • View Profile
[SOLVED] sf::Shape pointers
« 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));

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
[SOLVED] sf::Shape pointers
« Reply #1 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.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
[SOLVED] sf::Shape pointers
« Reply #2 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));
Laurent Gomila - SFML developer

danthebeliever

  • Newbie
  • *
  • Posts: 10
    • View Profile
[SOLVED] sf::Shape pointers
« Reply #3 on: March 24, 2011, 10:04:23 pm »
Great! That works! Thank you very much :)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
[SOLVED] sf::Shape pointers
« Reply #4 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.
Laurent Gomila - SFML developer

danthebeliever

  • Newbie
  • *
  • Posts: 10
    • View Profile
[SOLVED] sf::Shape pointers
« Reply #5 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.

 

anything