tomab tomab - 1 year ago 119
C++ Question

Insert new pair in std::map<CString, CString>

I have two lists and a name to find. If the name to find is not in the first list then it might be in the second list with a slightly different format. Conversion function between the two formats are given.

std::map<CString, CString>* convertedNames;

BOOL CSome::SeekNameWithConversion(std::set<CString> names, CString nameToFind)
for (auto it = names.begin(); it != names.end(); ++it)
if (nameToFind.Compare(*it) == 0) return true;

auto convertedIt = convertedNames->find(*it);
if (convertedIt != convertedNames->end() &&
nameToFind.Compare(convertedIt->second) == 0)
return true;

CString justConvertedName = ConvertToTheOtherFormat(nameToFind);
convertedNames->insert(*it, justConvertedName); // Error here
return nameToFind.Compare(justConvertedName) == 0;

The error which appears is:

error C2675: unary '++':
'ATL::CStringT<char,StrTraitMFC_DLL<char,ATL::ChTraitsCRT<_CharType>>>' does
not define this operator or a conversion to a type acceptable to the
predefined operator

I would like to know why the operator
is involved here and then how should I treat this error.

Answer Source

Most of the various insert functions of std::map require an iterator. Instead, you pass the pointed-to-object, (which is a CString, I suppose):

convertedNames->insert(*it, justConvertedName);
                       this is a CString, not a std::map<CString,CString>::iterator

If you want to insert a key-value pair, use the map's value_type instead which is basically a std::pair made up of key and value:

convertedNames->insert(std::make_pair(*it, justConvertedName));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download