Problem with vector <T> .erase () with g ++
The following code builds the error on msvc but does not compile on g ++.
if(*it == listener){ it = listeners.erase(it); } Erase error, Full code:
void AguiListener::removeListener( AguiWidget* listener ) { for(std::vector<AguiWidget*>::const_iterator it = listeners.begin(); it != listeners.end(); ++it) { if(*it == listener){ it = listeners.erase(it); } } } and listeners:
std::vector<AguiWidget*> listeners; but g ++ spits out a lot of errors:
AguiListener.cpp:29: error: nomatching function for call to'std::vector<AguiWidget*,std::allocator<AguiWidget*>>::erase(__gnu_cxx::__normal_iterator<AguiWidget*const*, std::vector<AguiWidget*,std::allocator<AguiWidget*> > >&)'/usr/include/c++/4.2.1/bits/vector.tcc:109:note: candidates are: typenamestd::vector<_Tp, _Alloc>::iteratorstd::vector<_Tp,_Alloc>::erase(__gnu_cxx::__normal_iterator<typenamestd::_Vector_base<_Tp,_Alloc>::_Tp_alloc_type::pointer, std::vector<_Tp, _Alloc> >) [with _Tp= AguiWidget*, _Alloc = std::allocator<AguiWidget*>]/usr/include/c++/4.2.1/bits/vector.tcc:121:note: typenamestd::vector<_Tp, _Alloc>::iteratorstd::vector<_Tp,_Alloc>::erase(__gnu_cxx::__normal_iterator<typenamestd::_Vector_base<_Tp,_Alloc>::_Tp_alloc_type::pointer, std::vector<_Tp, _Alloc> >,__gnu_cxx::__normal_iterator<typename std::_Vector_base<_Tp,_Alloc>::_Tp_alloc_type::pointer, std::vector<_Tp, _Alloc> >) [with _Tp= AguiWidget*, _Alloc = std::allocator<AguiWidget*>]
What could be wrong? Also, why does it work on msvc but not g ++?
thanks
In the current C ++ (C ++ 03) standard, std::vector::erase accepts an iterator , not a const_iterator .
In the upcoming C ++ (C ++ 0x) standard, std::vector::erase takes const_iterator . The implementation of the Visual C ++ Standard Library already supports this (at least in the latest version of Visual C ++ 2010).