0

尝试将 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 有。任何想法为什么会发生?

干杯托梅克

4

1 回答 1

3

当您将结束迭代器与运算符进行比较时,它将编译!=

于 2013-04-29T07:41:05.370 回答