Search code examples
c++pointersvectorerase

Error when trying to erase vector of pointers


Lets say I have a vector of class pointers Tower like this:

vector<Tower*> tower_list;

I have done a couple operations to add Towers into my vector but now when I try to use the vector::erase method like this

tower_list.erase(0);

I get the following error message:

Error: no instance of overloaded function "std::vector<_ty, _Alloc>::erase[with _Ty=Tower*, _Allow=std:allocator<Tower*>]" matches the argument list argument types are: (int)
object type is: std::vector<Tower*, std::alocator<Tower*>>

Can anyone explain why I am receiving this compile error when trying to erase this tower pointer? Let me know if you need more details. Thanks


Solution

  • vector::erase takes an iterator, not an integer.

    So if you want to erase the first element in the vector:

    tower_list.erase (tower_list.begin());
    

    http://en.cppreference.com/w/cpp/container/vector/erase