There are a few problems:
1) No need to erase each element one at a time like that. vector's dtor will take care of that automatically. You don't have to worry about cleanup. Or if you want to do it explicitly,
ClassAList.clear();
will erase all elements.
2) Calling erase() invalidates the iterator. So even if this code compiled, it would not work because 'inter' would become a bad iterator after the first time the loop is run. This is typically solved by using the return value of erase() as the new iterator.
3) Erasing a vector from the first element to the last is criminally inefficient, since the entire vector would need to be reallocated and moved for each item that is removed. Removing back to front is better (or, as stated earlier, just clear() or have the dtor do it for you automatically)
4) Erase takes an
iterator. You're giving it an instance of your class, which is why you're getting a compiler error. Something like this is probably what you were trying to do:
ClassAList.erase(inter); // note: no *
If you want to manually remove each element in a vector, here are your options (listed best to worst)
1 (Best): Do nothing, let vector's dtor do it:
2: Clear the vector:
3: Spin on a loop that pop_back's elements until all elements are removed:
1 2
|
while(!ClassAList.empty())
ClassAList.pop_back();
|
4 (worst): erase individual elements:
1 2 3
|
std::vector<A>::iterator i = ClassAList.begin();
while(ClassAList.empty())
i = ClassAList.erase(i);
|
Note that we never do ++i because erase returns the new iterator to use.