1 Replies - 153 Views - Last Post: 28 January 2013 - 09:54 AM Rate Topic: -----

#1 Wildras  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 28-January 13

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

Posted 28 January 2013 - 09:48 AM

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.

Is This A Good Question/Topic? 0
  • +

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

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8955
  • View blog
  • Posts: 33,573
  • Joined: 12-June 08

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

Posted 28 January 2013 - 09:54 AM

Closing triplicate topic.. watch out for that.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1