2012-07-11 84 views
3

在此代碼:矢量擦除方法失敗

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 

有誰知道我如何刪除這些數據?

回答

8

std::vector::erase單參數版本預計一個迭代器,而你傳遞一個元素的地址。

要刪除正確的元素,您需要爲該向量傳遞有效的迭代器。你可以使用m_ObjectList.begin()和一個增量來構造一個,或者通過使用std::advance來累加增量。

m_ObjectList.erase(std::advance(m_ObjectList.begin(), Index)); 
6
m_ObjectList.erase(m_ObjectList.begin() + Index);