Konrad
Konrad

Reputation: 41027

Returning an Iterator

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> &'

Upvotes: 2

Views: 10985

Answers (3)

Dima
Dima

Reputation: 39429

You should also look at std::find_if() function. It may be a cleaner way to do this.

Upvotes: 0

Ferruccio
Ferruccio

Reputation: 100748

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;

Upvotes: 6

korona
korona

Reputation: 2230

Sounds like you have your const_iterators mixed up. Please post more code, specifically how you are declaring your iterator.

Upvotes: 0

Related Questions