tags:

views:

1463

answers:

4

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> &'
+4  A: 

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;
Ferruccio
Thanks! I knew it was something silly. :)
Konrad
A: 

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

korona
A: 

Hard to say, since you haven't shown what 'it' is.

jalf
A: 

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

Dima