在此代码中:
template <class TP> TP GCVVector<TP>::Remove( long Index )
{
TP Result = m_ObjectList.at( Index );
m_ObjectList.erase( &m_ObjectList.at( Index ) );
return Result;
}
我收到编译时错误:
/trnuser1/rmtrain/DevelopmentEnv/Telstra/USM/../../Generic/CoreObjects/GCVVector.h: In member function âTP GCVVector<TP>::Remove(long int) [with TP = GCVString]â:
/trnuser1/rmtrain/DevelopmentEnv/Generic/ModulePopulator/GCVMPState.cpp:69: instantiated from here
/trnuser1/rmtrain/DevelopmentEnv/Telstra/USM/../../Generic/CoreObjects/GCVVector.h:241: error: no matching function for call to âstd::vector<GCVString, std::allocator<GCVString> >::erase(long int&)â
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/vector.tcc:110: 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 = GCVString, _Alloc = std::allocator<GCVString>]
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/vector.tcc:122: 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 = GCVString, _Alloc = std::allocator<GCVString>]
make[2]: *** [CMakeFiles/GCVMP.dir/trnuser1/rmtrain/DevelopmentEnv/Generic/ModulePopulator/GCVMPState.o] Error 1
make[1]: *** [CMakeFiles/GCVMP.dir/all] Error 2
有谁知道如何删除数据?
答案 0 :(得分:8)
std::vector::erase的单个参数版本需要迭代器,并且您传递元素的地址。
要删除正确的元素,您需要为该向量传递有效的迭代器。您可以使用m_ObjectList.begin()
构建一个增量,并使用std::advance对增量进行求和。
m_ObjectList.erase( std::advance(m_ObjectList.begin(), Index) );
答案 1 :(得分:7)
m_ObjectList.erase(m_ObjectList.begin() + Index );