subodh1989
subodh1989

Reputation: 716

Vector erase method failing

In this code :

template <class TP> TP GCVVector<TP>::Remove( long Index )
{

    TP Result = m_ObjectList.at( Index );

    m_ObjectList.erase( &m_ObjectList.at( Index ) );

    return Result;
}

I am getting compile time error :

/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

Does anyone know how do i remove the data?

Upvotes: 3

Views: 4364

Answers (2)

juanchopanza
juanchopanza

Reputation: 227370

The single argument version of std::vector::erase expects an iterator, and you are passing the address of an element.

To remove the right element, you need to pass a valid iterator for that vector. You can construct one using m_ObjectList.begin() and an increment, either by summing the increment, of by using std::advance.

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

Upvotes: 8

Jeeva
Jeeva

Reputation: 4663

m_ObjectList.erase(m_ObjectList.begin() + Index );

Upvotes: 7

Related Questions