Reputation: 1965
Please bear with me if the question is silly.
The following are defined in a header file :
typedef char NAME_T[40];
struct NAME_MAPPING_T
{
NAME_T englishName;
NAME_T frenchName;
};
typedef std::vector<NAME_MAPPING_T> NAMES_DATABASE_T;
Later the need comes that a particular english name be found :
const NAMES_DATABASE_T *wordsDb;
string str;
std::find_if( wordsDb->begin(),
wordsDb->end(),
[str](const NAME_MAPPING_T &m) -> bool { return strncmp(m.englishName, str.c_str(), sizeof(m.englishName)) == 0; } );
This code (which I copy-pasted to be honest) compiles, but if I want to check the value returned by find_if(), like this :
NAMES_DATABASE_T::iterator it;
it = std::find_if(blah ..)
the code will NOT compile !
In effect the line it = std::find_if(...) will return the error :
error C2679: binary '=' : no operator found which takes a right-hand operand of type 'std::_Vector_const_iterator<_Myvec>' (or there is no acceptable conversion)
What is wrong ?
Thanks for your time.
Upvotes: 1
Views: 356
Reputation: 22020
const NAMES_DATABASE_T *wordsDb;
Your wordsDb
is const, therefore wordsDb->begin()
returns a const iterator, therefore find_if
returns a const iterator as well. You're trying to assign that const iterator to the non-const NAMES_DATABASE_T::iterator it
, hence the error.
You can use NAMES_DATABASE_T::const_iterator
to get a const iterator. And you should use std::string
instead of those char buffers, unless there are some rare circumstances that require otherwise.
Upvotes: 5