4 Replies - 287 Views - Last Post: 28 January 2013 - 03:39 PM 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:46 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 mojo666  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 352
  • View blog
  • Posts: 770
  • Joined: 27-June 09

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

Posted 28 January 2013 - 10:33 AM

It seems like you are deleting the object at the iterator, then trying to use that object which no longer exists. Thus the crash. Erase should fully delete any object associated with the node, so you shouldn't have to use delete on every node. When you have emptied the list and no longer need it, you can delete the actual objects.
Was This Post Helpful? 0
  • +
  • -

#3 Wildras  Icon User is offline

  • New D.I.C Head

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

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

Posted 28 January 2013 - 12:35 PM

"you shouldn't have to use delete on every node."
Are you saying I don't even need to use delete?
Its how I've been taught to delete a list of objects. This endevour is new to me but I'm doing my best to learn.

"Erase should fully delete any object associated with the node"
So I should first use erase and then the objects' destroy method?

"When you have emptied the list and no longer need it, you can delete the actual objects."
I dont quite understand how I can delete objects from an empty list?
Was This Post Helpful? 0
  • +
  • -

#4 #define  Icon User is online

  • Duke of Err
  • member icon

Reputation: 1329
  • View blog
  • Posts: 4,558
  • Joined: 19-February 09

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

Posted 28 January 2013 - 03:16 PM

I don't see what is causing the error. Adding some print/log statements can help show what is happening.

Here, I have looked at adding, removing and deleting items.


#include <iostream>
#include <string>
#include <list>

using namespace std;

class Item
{
  public:
    string name;

    Item(string n) : name(n ){
      cout << "creating object - " << name << endl;
    }

    ~Item() {
      cout << "deleting object - " << name << endl;
    }

    void print() {cout << " " << name << endl;}

    friend ostream& operator << (ostream& os, Item &item);

    bool compare(string n) {return n == name;}
};

ostream& operator << (ostream& os, Item &item)
{
  os << " " << item.name;
  return os;
}


class Inventory
{
  public:
    list<Item*> items;
    typedef list<Item*>::iterator list_iterator;
    list_iterator it;

    bool add(Item *item);
    void print();
    void clear();
    bool remove(const string &name);
    list_iterator find(list_iterator first, list_iterator last, string name);
};


bool Inventory::add(Item *item)
{
   cout << "adding item -" << *item << endl;
   items.push_back(item);
   return true;
}


void Inventory::clear()
{
  cout << "clearing list " << endl;
  for(it = items.begin(); it != items.end(); )
  {
     delete *it;
     it = items.erase(it);
  }
}

void Inventory::print()
{
  cout << "printing list " << endl;
  for(it = items.begin(); it != items.end(); it++)
  {
     (*it)->print();
  }
}

Inventory::list_iterator Inventory::find(list_iterator first, list_iterator last, string name)
{
 while (first!=last) {
    if ((*first)->compare(name))
      return first;
    first++;
  }
  return last;
}


bool Inventory::remove(const string &name)
{
   cout << "remove item " << name;
   it = find (items.begin(), items.end(), name);
   if(it == items.end()) {
     cout << " failure" << endl;
     return false;
   }
   items.erase(it);
   cout << " successful" << endl;
   return true;
}


int main()
{
  Inventory inventory;

  Item *tmp;

  tmp = new Item("sword");
  inventory.add(tmp);

  tmp = new Item("bow");
  inventory.add(tmp);

  // also can use anonymous Item object
  inventory.add(new Item("axe"));
  inventory.add(new Item("health potion"));

  inventory.print();
  inventory.remove("axe");
  inventory.remove("staff");

  inventory.print();
  inventory.clear();
  inventory.print();

  cout << "Press enter to continue. " << endl;
  cin.get();
  return(0);
}


This post has been edited by #define: 28 January 2013 - 03:31 PM

Was This Post Helpful? 0
  • +
  • -

#5 Wildras  Icon User is offline

  • New D.I.C Head

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

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

Posted 28 January 2013 - 03:39 PM

I actually just worked it out.
Before, in main, I had an Items list defined as so:
std::list<Item *> Items;
std::list<Item *>::iterator ItemIter;


I redefined it to:
std::list<Item> Items;
std::list<Item>::iterator ItemIter;


Then I passed a reference of this item list to Inventory class and did all the deeds with that list there instead of making the Inventory class have a separate list.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1