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

Author Topic: Adding shapes to vector  (Read 3183 times)

0 Members and 1 Guest are viewing this topic.

Blingu

  • Newbie
  • *
  • Posts: 15
    • View Profile
Adding shapes to vector
« on: November 06, 2018, 03:23:19 pm »
Hi, I want to make a class with a vector inside that storages shapes for me (e.g. a rectangle, circle). I know that sf::RectangleShape's and sf::CircleShape's base class is sf::Shape, so I made a class like that (includes are irrelevant here, they are fine):
//header file
class ObjectStorage
{
private:
        std::vector <std::shared_ptr<sf::Shape>>sequence;

public:
         ObjectStorage();
         void add(std::shared_ptr<sf::Shape> const &ob);
         void show();
};
//cpp file
void ObjectStorage::add(std::shared_ptr<sf::Shape> const &ob)
{
        sequence.push_back(ob);
}

As you can see whenever I want to call the method add with a rectangle or circle shape called test1 I do it like that (objectstorage is a pointer that is initialized with the ObjectStorage constructor, no problems with it as well):

objectstorage->add(test1);

Although it gives me this compiler error "no suitable user-defined conversion from "sf::RectangleShape" to "const std::shared_ptr<sf::Shape>" exists".
How can I store rectangle, convex and circle shapes in a single vector?

Tigre Pablito

  • Full Member
  • ***
  • Posts: 226
    • View Profile
    • Email
Re: Adding shapes to vector
« Reply #1 on: November 07, 2018, 03:19:39 am »
Hi,

I'm sure that it works with an array, but I'm not sure if it works with a vector (List<T> in C#)

i.e. (in C#)

Parent[] array = new Parent[] { new Child1(), new Child2(), new Child1(), new Child4() };

Or maybe if you don't make the generic type "const" ... ?  ::)

........

What has happened with your Arkanoid clone project? Remember that Hapax and i tried to help you?  ;D
(sorry for the out of topic)

Blingu

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: Adding shapes to vector
« Reply #2 on: November 07, 2018, 04:49:37 pm »
Hi,

I'm sure that it works with an array, but I'm not sure if it works with a vector (List<T> in C#)

i.e. (in C#)

Parent[] array = new Parent[] { new Child1(), new Child2(), new Child1(), new Child4() };

Or maybe if you don't make the generic type "const" ... ?  ::)

........

What has happened with your Arkanoid clone project? Remember that Hapax and i tried to help you?  ;D
(sorry for the out of topic)

I abandoned it for now due to the issue I still couldn't solve. Maybe I'll just use a sprite with rounded edges like another project does where this issue doesn't appear.

I'll try to implement your suggestion to my code and gonna see if it works.

Blingu

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: Adding shapes to vector
« Reply #3 on: November 07, 2018, 05:05:27 pm »
I don't really know how to do that, nor how to operate with an array like this afterwards. Somebody got an idea how to do it with vectors?  :-\

Or can somebody explain me what is the problem with my stuff above?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Adding shapes to vector
« Reply #4 on: November 07, 2018, 07:36:01 pm »
The error message is pretty clear, a sf::Rectangle is not a std::shared_ptr<sf::Shape>, and is not convertible to it.

If you want to store non-owning pointers, then use std::vector<sf::Shape*>. If you want your vector to own the shapes, use a std::vector<std::unique_ptr<sf::Shape>>. How to properly use each solution is pure C++ stuff, that you can learn outside this SFML forum ;)
Laurent Gomila - SFML developer

 

anything