开发者

Problem with vector<T>.erase() with g++

The following code builds error free on msvc, but does not compile on g++.

         if(*it == listener){
            it = listeners.erase(it);
        }

The error is in erase, The full code is:

void AguiListener::removeListener( AguiWidget* listener )
{
    for(std::vector<AguiWidget*&开发者_如何学Pythongt;::const_iterator it = listeners.begin(); 
        it != listeners.end(); ++it)
    {
        if(*it == listener){
            it = listeners.erase(it);
        }
    }
}

and listeners is:

std::vector<AguiWidget*> listeners;

but g++ spits out loads of errors:

AguiListener.cpp:29: error: no matching 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: typename std::vector<_Tp, _Alloc>::iterator std::vector<_Tp, _Alloc>::erase(__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*>] /usr/include/c++/4.2.1/bits/vector.tcc:121: note: typename std::vector<_Tp, _Alloc>::iterator std::vector<_Tp, _Alloc>::erase(__gnu_cxx::__normal_iterator<typename std::_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 with this? Also, why would it work on msvc but not g++?

Thanks


In the current C++ standard (C++03), std::vector::erase takes an iterator, not a const_iterator.

In the forthcoming C++ standard (C++0x), std::vector::erase takes a const_iterator. The Visual C++ Standard Library implementation already supports this (at least it does in the latest version, Visual C++ 2010).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜