Austin Seymour
Austin Seymour

Reputation: 15

List Iterator not Dereferenceable Run time

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;
    }
}

};

The problem happens in the for loop of render when I try to dereference the iterator.

I have checked that the list is not empty I can access the first and last element of the list but for some reason the for loop always throws a list Iterator not dereferenceable.

Upvotes: 1

Views: 43

Answers (1)

Jonathan Potter
Jonathan Potter

Reputation: 37132

The problem is in this line:

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

You have a spurious semi-colon at the end of the for clause. Easy fix - get rid of it.

If you're curious as to why this causes a problem - what this means is that the entire loop will complete before it proceeds to execute the code in the body of the loop (because that code isn't actually in the body of the loop after all). And once it gets to that code, itr equals mPortalList.end().

Upvotes: 1

Related Questions