【发布时间】:2013-04-22 19:29:58
【问题描述】:
我在尝试将 std::map 与我自己的类用作值时出错。地图的定义是这样的:
std::map<std::string,CCrossSection> Xsects;
这一行编译得很好(所以它有点工作?)
Xsects[sectionId].m_vProfile.push_back(pt);
当我尝试迭代地图时:
for (std::map<std::string,CCrossSection>::iterator xs = Xsects.begin(); xs < Xsects.end(); xs++) {
it->second.SaveFile(f);
}
它给了我多个类似这样的错误:
error C2784: 'bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Traits> &)' : could not deduce template argument for 'const std::_Tree<_Traits> &' from 'std::_Tree<_Traits>::iterator'
with
[
_Traits=std::_Tmap_traits<std::string,CCrossSection,std::less<std::string>,std::allocator<std::pair<const std::string,CCrossSection>>,false>
]
c:\program files\microsoft visual studio 9.0\vc\include\xtree(1466) : see declaration of 'std::operator <'
我认为这是 less 运算符的问题,我将它添加到我的 CCrossSection 类的定义中,但它并没有改变任何事情。后来我读到地图的键必须定义较少的运算符,我认为 std::string 有。任何想法为什么会发生?
干杯 托梅克
【问题讨论】:
-
不确定,但我认为这是因为您正在执行 xs
-
根据 Vincent 的回答,我查看了 cplusplus.com/reference/iterator/iterator 运算符 '