C++;列表-添加项目 我是C++新手,使用列表有问题。我不明白为什么我会在下面的示例中出错

C++;列表-添加项目 我是C++新手,使用列表有问题。我不明白为什么我会在下面的示例中出错,c++,list,C++,List,GameObject类是一个抽象类 玩家类和子弹类继承游戏对象类 list<GameObject*> gameObjects = list<GameObject*>(); gameObjects.push_front(&player); while(gameLoop) { if (canShoot) { Bullet b = Bullet(player.Position.X , player.Position.Y);

GameObject类是一个抽象类 玩家类和子弹类继承游戏对象类

list<GameObject*> gameObjects = list<GameObject*>();
gameObjects.push_front(&player);
while(gameLoop)
{
    if (canShoot)
    {
        Bullet b = Bullet(player.Position.X , player.Position.Y);
        gameObjects.push_front(&b);
    }   
    for each (GameObject *obj in gameObjects)
    {
        (*obj).Update(); // get an error
    }
}
list gameObjects=list();
游戏对象。向前推(&P);
while(gameLoop)
{
如果(可以拍摄)
{
项目符号b=项目符号(player.Position.X,player.Position.Y);
游戏对象。向前推(&b);
}   
每个(游戏对象中的游戏对象*obj)
{
(*obj.Update();//获取一个错误
}
}

错误是Debug error-Abort()已被调用。

您的
foreach
语法完全错误,实际上,更重要的是,要循环列表中的每个元素,请执行以下操作:

for (GameObject *obj : gameObjects)
{
   obj->Update(); 
}
或者,C++11之前的版本:

for(std::list<GameObject*>::iterator itr = gameObjects.begin(); itr != gameObjects.end(); ++itr)
{
  (*itr)->Update();
}

哪个错误?你能说得更具体一点吗?@Baltasarq我猜他在一个充满废弃物体的向量中拉屎的错误是什么?可能是segfaults为什么需要
list gameObjects=list()?我指的是初始化部分。当然,他是在堆栈中创建对象,而不是在堆中。嘿,“每人”是什么意思?无论如何,如果他正在寻求帮助,他可以提供更多的信息…
(*obj).Update()
可以很容易地写成
obj->Update()
谢谢你的回答:)。但现在编译器告诉我:错误C2259'GameObject':无法实例化抽象类
list<GameObject*> gameObjects;

while(gameLoop)
{
    if (canShoot)
    {
        Bullet* b = new Bullet(player.Position.X , player.Position.Y);
        gameObjects.push_front(b);
    }   
    for (GameObject* obj : gameObjects)
    {
        obj->Update();
    }
}