2

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;
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
bbdude95
  • 240
  • 1
  • 2
  • 12
  • have you tried define the functions you use as const? You use a const iterator.http://stackoverflow.com/questions/309581/const-and-nonconst-stl-iterators, if you change something with your iterator by calling the member function maybe it causes the problem, try just "cout <<" it, or try with a non const iterator. – David Szalai Nov 06 '13 at 18:39

3 Answers3

5

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.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
2

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
1

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).

Syntactic Fructose
  • 18,936
  • 23
  • 91
  • 177