1

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.

1
  • 1
    Is there a reason in using char arrays for the names instead of std::string? Commented Jul 6, 2012 at 6:52

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.