【发布时间】:2013-11-18 03:38:48
【问题描述】:
我正在尝试为我的项目符号使用列表。 每当我运行更新项目符号代码时,它都会给我这个错误:
错误 1 错误 C2662:“Bullet::detectCollision”:无法将“this”指针从“const Bullet”转换为“Bullet &”
我的清单:
std::list<Bullet> bullet_list;
我的更新代码:
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
}
}
我的检测碰撞代码:
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;
}
【问题讨论】:
-
您是否尝试过将您使用的函数定义为 const?您使用 const 迭代器。stackoverflow.com/questions/309581/…,如果您通过调用成员函数使用迭代器更改某些内容,可能会导致问题,请尝试“cout