jeudi 12 novembre 2020

How can an instance add itself to a vector?

I need to instantiate a class SpriteWithTimer and that the new object adds itself to a vector vsprites.

Here's a code snippet:

#include <vector>
...
class SpriteWithTimer {
   public:
   SpriteWithTimer();
   ~SpriteWithTimer();
   ...
};
static std::vector<SpriteWithTimer> vsprites;

int main()
{
   ...
}
...
SpriteWithTimer::SpriteWithTimer(){
        ...
        vsprites.push_back(this);
   };

but I've got his error:

no matching function for call to ‘std::vector<SpriteWithTimer>::push_back(SpriteWithTimer*)’
no instance of overloaded function "std::vector<_Tp, _Alloc>::push_back [with _Tp=SpriteWithTimer, _Alloc=std::allocator<SpriteWithTimer>]" matches the argument list -- argument types are: (SpriteWithTimer *) -- object type is: std::vector<SpriteWithTimer, std::allocator<SpriteWithTimer>>

thanks!

Aucun commentaire:

Enregistrer un commentaire