List Iterator not Dereferenceable运行时

时间:2015-12-13 05:40:18

标签: c++ runtime

portalManager::portalManager(SDL_Renderer* argRenderer)
{
    mLootManager = new lootManager();
    mLootManager->initialize();
    lootPortal* newPortal;
    newPortal = new lootPortal(128, 128, Portal::eForest, Tile::eForest);
    mPortalList.push_back(newPortal);
    newPortal = new lootPortal(256, 256, Portal::eForest, Tile::eForest);
    mPortalList.push_back(newPortal);
    mPortalSheet = new spriteSheet(192, 192, 0);
    mPortalSheet->loadTexture("Images/Portals.png", argRenderer);
    mRenderQuad.w = Tile::cTileSize;
    mRenderQuad.h = Tile::cTileSize;
    mTextureQuad.w = Tile::cTileSize;
    mTextureQuad.h = Tile::cTileSize;
}

void portalManager::render(int argX, int argY, int argW, int argH, SDL_Renderer* argRenderer)
{

std::list<lootPortal*>::const_iterator itr = mPortalList.begin();
for (itr = mPortalList.begin(); itr != mPortalList.end(); itr++);
{
    std::cout<<(*itr)->getX()<<std::endl;
    mRenderQuad.x = (*itr)->getX();
    mRenderQuad.y = (*itr)->getY();
    if ((mRenderQuad.x >= argX && mRenderQuad.x <= argX+argW) && (mRenderQuad.y >= argY && mRenderQuad.y <= argY + argH))
    {

        mTextureQuad.x = 0;
        mTextureQuad.y = 0;
    }
}

};

当我尝试取消引用迭代器时,问题发生在for循环中。

我检查过列表不是空的我可以访问列表的第一个和最后一个元素但是由于某种原因,for循环总是抛出一个列表Iterator而不能解引用。

1 个答案:

答案 0 :(得分:1)

问题出在这一行:

for (itr = mPortalList.begin(); itr != mPortalList.end(); itr++);
                                                                ^

你在for子句的末尾有一个虚假的分号。轻松修复 - 摆脱它。

如果您对这导致问题的原因感到好奇 - 这意味着整个循环将完成之前它继续执行循环体中的代码(因为毕竟,该代码实际上并不在循环体中。一旦获得该代码,itr就等于mPortalList.end()