我有两个名单和一个名字要找。如果要查找的名称不在第一个列表中,那么它可能在第二个列表中,格式略有不同。给出了两种格式之间的转换函数。
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;
}
}出现的错误是:
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我想知道为什么操作符++在这里,然后我应该如何处理这个错误。
发布于 2016-10-05 13:49:01
大多数insert函数的std::map都需要迭代器。相反,传递指向对象(我想这是一个CString ):
convertedNames->insert(*it, justConvertedName);
^^^
this is a CString, not a std::map<CString,CString>::iterator如果要插入键值对,请使用映射的value_type (基本上是由键和值组成的std::pair ):
convertedNames->insert(std::make_pair(*it, justConvertedName));发布于 2016-10-05 13:47:46
map::insert的第一个参数是迭代器,而不是CString。在内部,该方法试图增加迭代器。这显然是给operator++打了个电话。您不需要使用这个插入重载。当您知道将插入元素的位置附近时,它的目的是提高性能。代之以打电话给convertedNames->insert(std::make_pair(*it, justConvertedName))。
发布于 2016-10-05 13:46:37
因此,首先要理解的是,模板总是在标头中完全实现,因为所需的任何类都是用该对象构建的(只需想一想,如果std库中包含了所有可能的std::vector!)
这意味着模板的实现是公开的-在本例中,在某个地方有一个++。如果您将整个错误打印出来(这将是相当多的几行),您甚至可能会被告知您错误的参数。
无论如何,我们可以看到,它显然是一个CString,但我猜这是
..。转换为其他格式
很可能没有把你想的东西还回去
https://stackoverflow.com/questions/39875536
复制相似问题