Hello community, I always have had problems with this kind of object, the thing is that I am following a tutorial http://seryusjj.github.io and this guy manages vectors to pointers to subclassed Sprites as a cocos::Vector, the problem is that when you try to pass the Vector as a reference to a member function and try to copy elements from it gives a lot of kind of errors in compilation.
For example, this is the code of the function
void Player::setTargets(cocos2d::Vector<BasicEnemy *> &targets){
for(int i; i < targets.size(); ++i)
_targets.pushBack(targets.at(i));
for (int i = 0; i < _numBullets; i++){
_bulletPool.at(i)->setPlayerTargets(_targets);
}
}
and this is the errors I get…
error: static_assert failed due to requirement 'std::is_convertible<BasicEnemy *, cocos2d::Ref *>::value' "Invalid Type for cocos2d::Vector<T>!"
static_assert(std::is_convertible<T, Ref*>::value, "Invalid Type for cocos2d::Vector<T>!");
error: member access into incomplete type 'BasicEnemy'
Does your BasicEnemy class inherit from a cocos2d::Node or more specifically cocos2d::Ref?
Cocos Vector can only contain Ref objects or Ref subclass objects.
The assertion is implying that your BasicEnemy class is not implicitly convertible to a cocos Ref which is required for the cocos Vector.
Can you show us your BasicEnemy .h/.hpp file? There is also a potential circular dependency seen from the second error on incomplete type.
I think you might have a circular dependency among the Player and BasicEnemy classes. I do not see your Player.h and cpp files, but I am pretty sure that is the issue. Read up a bit about circular dependency and see if this is your scenario and try fix it. That is my next guess as it looks like your BasicEnemy class does inherit from Ref which was my first guess to look at.
I checked on the classes files this guy from the tutorial http://seryusjj.github.io worked on, and compared on mines and yes, it was a circular problem. I was missing an #include in the Player.h file, after that the project have compiled perfect. THANK YOU!
So then, there is no trouble using pointers as members of a cocos2d::Vector?
Because in the past with personal projects I had this kind of troubles also, but then probably, I also had circular dependencies, and tought that cocos2d-x is a hard framework to use.
cocos2d::Vector is a specialized container that expects pointers to objects of type cocos2d::Ref, which are reference counted objects. If you try to add any other type of object, it won’t work. It was made so it would hold Ref objects and ensure that they are not freed/destroyed until you are ready to destroy them.
When an object of type Ref is added to the vector, it calls retain() on it, which increments the reference count, and when an object is removed from the vector, it calls release(), which decrements the reference count.
That’s pretty much it. If you want to store any other kind of object, then just use a std::vector. Also, nothing is stopping you from using a std::vector for cocos2d::Ref* either, but then you’ll be responsible for controlling their lifetime (through retain/release, if required).