C++ Return sprite object from class in vector
Question
I have an Entity baseclass which the classes Player and Enemy Inherit. Both player and enemy contain a sprite object (From the SFML api) that looks like this:
class Player : Entity
{
sf::Sprite sprite
}
Player and Enemy are created inside a vector which is set up like this:
class EntityManager
{
public:
void CollisionCheck();
private:
std::vector<Entity*> entityVector;
}
I'm looking to use a collision detection function which is of this form:
bool Collision::CircleTest(const sf::Sprite& Object1, const sf::Sprite& Object2)
So I'm trying to do something like this:
void EntityManager::ColCheck()
{
if (Collision::CircleTest(entityVector[0].sprite, entityVector[1].sprite))
{
cout << "COLLISION\n";
}
}
But I get this compile error:
error: request for member ‘sprite’ in
‘((EntityManager*)this)->EntityManager::entityVector.std::vector<_Tp, _Alloc>::operator[] with _Tp = Entity*, _Alloc = std::allocator’, which is of non-class type ‘Entity*’
How do I pass the sprite object from these classes inside the vector to the collision function?
Solution
Since entityVector
holds Entity*
you'll need to use the proper syntax: entityVector[0]->sprite
instead of entityVector[0].sprite
.