Returning an Iterator

10,700

Your search function is returning a const_iterator. You should either return the same type, i.e. std::vector< CClass >::const_iterator, or cast it to a std::vector< CClass >::iterator if you intend the caller to be able to modify the found item through the iterator.

EDIT: after seeing your update, it seems the problem is your iterator (it) has a different type than your function return. They should be the same.

std::vector< CClass >::iterator it;
Share:
10,700
uss
Author by

uss

About me.. check out my blog. :)

Updated on June 04, 2022

Comments

  • uss
    uss almost 2 years

    I have a function which searches an STL container then returns the iterator when it finds the position, however I am getting some funny error messages, can tell me what I am doing wrong?

    Function:

    std::vector< CClass >::iterator CClass::SearchFunction( const std::string& strField )
    {
    ...
    
       return it;
    
    ...
    }
    

    Error:

    error C2664: 'std::_Vector_iterator<_Ty,_Alloc>::_Vector_iterator(const std::_Vector_iterator<_Ty,_Alloc> &)' : cannot convert parameter 1 from 'std::_Vector_const_iterator<_Ty,_Alloc> *__w64 ' to 'const std::_Vector_iterator<_Ty,_Alloc> &'