Well, first, before you call advance, you have to make sure iter points to the first element.
Second, when you call erase on an iterator, you invalidate it, so you can't increment it anymore.
However, erase returns the next valid iterator, so you can do this:
Not sure what you mean.
You do it like you would do it anywhere else... but that seems obvious and certainly doesn't involve inheritance, so you might want to elaborate.
Just a small comment on the use of vector. A vector provides random access, which is what you probably want. The catch is that vectors can have expensive insertion/deletion because they are stored contiguously. A deque may be a better choice, IF there will be a lot of insertion/deletion.
I agree with moorecm -- that you should use a deque, rather than a list. Then you can ditch the advance.
You aging loop doesn't look quite right. You need
iter = bunny_list.erase(iter);
rather than just
bunny_list.erase(iter);
( Note that you don't need a lambda function to use std::remove_if: you can use a function or (preferably) a functor instead. So you don't have to wait for C++0x (or use Boost) )
I would prob. use a global ofstream in this case. A global logger is one of the few exceptions I make to the "no globals" edict. (I am assuming that the lifetime of all your bunny objects is limited to main)
class TimeToDie
: public unary_function<bunny&, bool> // from <functional>
{
public:
// bunny ref is not const as it has to age
booloperator()(bunny& b) const
{
b.aging();
return ( ( b.getVamp() && (b.getAge()==50))
|| (!b.getVamp() && (b.getAge()==10)) );
}
};
// remove_if is from <algorithm>
bunny_list.erase(remove_if(bunny_list.begin(), bunny_list.end(), TimeToDie()), bunny_list.end());
You said you didn't know where the error is/was. More logging would have prob. helped you locate the prob.
Btw - you shouldn't delete content from the thread, as it makes the replies meaningless. So someone reading the thread in the future won't find it much use.
You said you didn't know where the error is/was. More logging would have prob. helped you locate the prob.
Btw - you shouldn't delete content from the thread, as it makes the replies meaningless. So someone reading the thread in the future won't find it much use.
Andy
Thanks for the advice. I usedcoutto get whats happening and it seem to help alot!