Reputation: 250
I am trying to use a list for my bullets. Whenever I run my update bullet code it gives me this error:
Error 1 error C2662: 'Bullet::detectCollision' : cannot convert 'this' pointer from 'const Bullet' to 'Bullet &'
my list:
std::list<Bullet> bullet_list;
my update code:
std::list<Bullet>::const_iterator cIter;
for ( cIter = bullet_list.begin( ); cIter != bullet_list.end( ); cIter++ )
{
if((*cIter).detectCollision(monster.position,monster.getHeight(),monster.getWidth()))
{
//Do stuff here
}
if ((*cIter).detectCollision(boss.getPosition(),boss.getHeight(),boss.getWidth()))
{
//Do stuff here
}
}
my detect collision code:
bool Bullet::detectCollision(Vector objTwo,int heightT,int widthT)
{
if(position.getVectorX() >= objTwo.getVectorX() - (widthT/2) && position.getVectorX() <= objTwo.getVectorX() + (widthT/2)
&& position.getVectorY() >= objTwo.getVectorY() - (widthT/2)&& position.getVectorY() <= objTwo.getVectorY() + (heightT/2) && alive)
{
return false;
}
return true;
}
Upvotes: 2
Views: 1486
Reputation: 310980
You are using const_iterator but trying to access a non-const member function. Either use iterator instead of the const_iterator or declare the function as const if it can be a const member function.
Upvotes: 2
Reputation: 20076
A const_iter
does not allow the values it points at to be changed, so when you're calling detectCollision
you need to promise the compiler you also won't change those values(make the function const
).
Upvotes: 1
Reputation: 308196
You need to declare detectCollision
as const
.
bool Bullet::detectCollision(Vector objTwo,int heightT,int widthT) const
When you don't, it's trying to do a conversion so it can call a non-const function on a const reference but it's not allowed.
Upvotes: 5