以下代码在msvc上构建无错误,但不在g ++上编译.
if(*it == listener){
it = listeners.erase(it);
}
Run Code Online (Sandbox Code Playgroud)
错误是擦除,完整代码是:
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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
和听众是:
std::vector<AguiWidget*> listeners;
Run Code Online (Sandbox Code Playgroud)
但是g ++吐出了很多错误:
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*>]
这可能有什么问题?另外,为什么它会在msvc上运行而不是g ++?
谢谢
在当前的C++标准(C++ 03)中,std::vector::erase取一个iterator而不是一个const_iterator.
在即将推出的C++标准(C++ 0x)中,std::vector::erase需要一个const_iterator.Visual C++标准库实现已经支持这一点(至少在最新版本Visual C++ 2010中).
| 归档时间: |
|
| 查看次数: |
1305 次 |
| 最近记录: |