Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

How do I completely get rid of this list? RPG game

$
0
0
I'm having trouble getting rid of an STD::List iterator. Before I was able to do this:

 
std::list<GameObject *> objects;
std::list<GameObject *>::iterator iter;



The list gets populated, etc. Now when I want to delete it I do this:

 
for(iter = objects.begin(); iter != objects.end();)/>
{
    (*iter)->Destroy();
    delete (*iter);
    iter = objects.erase(iter);
}



The code which I will paste further down is inside a class called Inventory. It gets its items via function AddItem():
fyi:I did try push_back, got the same result.

 
bool Inventory::AddItem(Item *item)
{
    if(maxWeight - item->GetWeight() >= 0)
    {
        maxWeight -= item->GetWeight();
        InventoryList.emplace_back(item);
        return true;
    }
    else
        return false;

} 



Now, in main, AddItem() is called like this:

for(ItemIter = Items.begin(); ItemIter != Items.end();++ItemIter)
{
    if((*ItemIter)->GetOwnID() == WORLD)
    {
        if((*ItemIter)->CheckCollisions(player))
        {
            if(inventory->AddItem((*ItemIter)))
            {
                (*ItemIter)->SetOwnID(PLAYER);
            }
        }
    }
}



And heres the code thats giving me problems, I cant delete InvIter. If I try to do it, the game crashes on exit "Game.exe has stopped working..." Btw. this code here doesn't give me any errors.
Its only when I add "delete (*InvIter)"

void Inventory::Destory()
{
    for(InvIter = InventoryList.begin(); InvIter != InventoryList.end();)/>
    {
        (*InvIter)->Destroy();
        InvIter = InventoryList.erase(InvIter); 
    }
}



I'd also appreciate if someone could tell me how I could correctly send an item to inventorys' AddItem(). And after sending it, how to delete it from the list in main.

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>