C++ 从向量中删除指针

C++ 从向量中删除指针,c++,vector,iterator,C++,Vector,Iterator,我试图删除指向对象的指针,但我一直在破坏控制台(PS2),我没有因为控制台的设置方式而得到任何错误,所以我不太确定到底发生了什么 我已经列出了两行出错的代码,直到我添加了这两行代码,这才出错 for(listIter = m_downDirectionList.begin(); listIter != m_downDirectionList.end(); listIter++) { Projectile* proj = dynamic_cast<Projec

我试图删除指向对象的指针,但我一直在破坏控制台(PS2),我没有因为控制台的设置方式而得到任何错误,所以我不太确定到底发生了什么

我已经列出了两行出错的代码,直到我添加了这两行代码,这才出错

    for(listIter = m_downDirectionList.begin(); listIter != m_downDirectionList.end(); listIter++)
    {
        Projectile* proj = dynamic_cast<Projectile*>(*listIter);

        if (proj->getZWorldCoord() >= (defaultLevelDepth + zOffset))
        {
            proj->getPoolOwner()->releaseAProjectile(proj);
            //(*listIter) = NULL; // THIS ERRORS, also tried = 0.
            //listIter = m_downDirectionList.erase(listIter); // THIS ALSO ERRORS
        }

        else
        {
            (*listIter)->update(camera, zOffset);
        }
    }
这也会出错。

for(listIter=m_downDirectionList.begin();listIter!=m_downDirectionList.end();)
  m_downDirectionList.erase (listIter);
{ 射弹*proj=动态抛投(*listIter); 如果(proj->getZWorldCoord()>=(defaultLevelDepth+zOffset)) { 项目->getPoolOwner()->发布项目(项目); listIter=m_downDirectionList.erase(listIter); } 其他的 {//m_downDirectionList[p]->更新(摄像头,zOffset); (*listIter)->更新(摄像头、zOffset); 利斯特++ } }
同样正确且优雅(尽管我对++listIter的评论仍然有效:))应该注意的是,这是有效的,因为它是一个向量(是的,很清楚,但问题和答案在空间上相距很远…。我不知道
m_downDirectionList
到底是什么,或者
releaseAProjectile
做了什么。我也不知道什么样的错误,或者你的测试技术,或者错误是否一致。关于编辑。如果你指的是我的解决方案,我所做的不仅仅是添加那一行。我还将迭代器增量移动到else块。我本来打算在发帖后编辑我的答案并解释一下,但我已经解释了这个问题?
  m_downDirectionList.erase (listIter);
for(listIter = m_downDirectionList.begin(); listIter != m_downDirectionList.end(); )
    {
        Projectile* proj = dynamic_cast<Projectile*>(*listIter);

        if (proj->getZWorldCoord() >= (defaultLevelDepth + zOffset))
        {
            proj->getPoolOwner()->releaseAProjectile(proj);
            listIter = m_downDirectionList.erase(listIter);
        }

        else
        { //m_downDirectionList[p]->update(camera, zOffset);
            (*listIter)->update(camera, zOffset);
            listIter++
        }
    }