bbd*_*e95 2 c++ iterator list const-iterator
我正在尝试使用我的项目符号列表。每当我运行更新项目符号代码时,它都会出现以下错误:
错误 1 错误 C2662:“Bullet::detectCollision”:无法将“this”指针从“const Bullet”转换为“Bullet &”
我的列表:
std::list<Bullet> bullet_list;
Run Code Online (Sandbox Code Playgroud)
我的更新代码:
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
}
}
Run Code Online (Sandbox Code Playgroud)
我的检测碰撞代码:
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;
}
Run Code Online (Sandbox Code Playgroud)
您需要声明detectCollision为const.
bool Bullet::detectCollision(Vector objTwo,int heightT,int widthT) const
Run Code Online (Sandbox Code Playgroud)
如果不这样做,它会尝试进行转换,以便可以在 const 引用上调用非常量函数,但这是不允许的。
| 归档时间: |
|
| 查看次数: |
1312 次 |
| 最近记录: |