c++ - error C2664 - code compiles fine in VC6; not in VS 2010 -


i have typedef, class member vector using type , method using std::<vector>::erase().

#typedef dword wordno_t;  class cwordlist : public cobject { public: wordno_t* begin() { return m_words.begin(); } wordno_t* end()   { return m_words.end(); } void truncate (wordno_t *ptr) {   if (ptr == end())     return;   assert (ptr >= begin() && ptr < end());   // following line generates c2664   m_words.erase (ptr, end()); }  private:   std:vector<wordno_t> m_words; } 

the detailed error is:
error c2664: 'std::_vector_iterator<_myvec> std::vector<_ty>::erase(std::_vector_const_iterator<_myvec>,std::_vector_const_iterator<_myvec>)' : cannot convert parameter 1 'const wordno_t' 'std::_vector_const_iterator<_myvec>'

pretty new stl... appreciated.

i'm surprised begin , end compiling, shouldn't. std::vector (and friends) use iterators, not pointers. (though intended act similarly.)

in case, erase takes iterator, not pointer. because vectors contiguous, can make utility functions such, though:

template <typename t, typename a> typename std::vector<t, a>::iterator to_iterator(t* pptr, std::vector<t, a>& pvec) {     assert(pptr >= &pvec.front() && pptr <= &pvec.back());      return pvec.begin() + (pptr- &pvec[0]); }  template <typename t, typename a> typename std::vector<t, a>::const_iterator to_iterator(const t* pptr, const std::vector<t, a>& pvec) {     assert(pptr >= &pvec.front() && pptr <= &pvec.back());      return pvec.begin() + (pptr - &pvec[0]); } 

basically, find out how many elements pptr &pvec[0] (the first element), add pvec.begin(). (transform offset pointer , pointer start offset start.) operation o(1). , then:

void truncate (wordno_t *ptr) {     // note == end() bit in here anyway:     m_words.erase(to_iterator(ptr, m_words), end()); } 

Comments

Popular posts from this blog

windows - Why does Vista not allow creation of shortcuts to "Programs" on a NonAdmin account? Not supposed to install apps from NonAdmin account? -

c++ - How do I get a multi line tooltip in MFC -

unit testing - How to mock PreferenceManager in Android? -