2013-04-29 67 views
0

嘗試將std :: map與我自己的類用作值時發生錯誤。地圖的定義是這樣的:std ::將對象作爲值的地圖

std::map<std::string,CCrossSection> Xsects; 

此行編譯罰款(?所以kindo作品)

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 <' 

我認爲這是一個少運算符的問題,我將它添加到我的類CCrossSection的定義中,但它沒有改變一件事。後來我讀到,映射的關鍵字必須少運算符定義,我認爲std :: string有。任何想法爲什麼會發生?

乾杯 託梅克

+0

不知道,但我想這是因爲你在做XS Vincent 2013-04-29 07:41:42

+0

基於Vincent的回答,我看了一下http://www.cplusplus.com/reference/iterator/iterator/ 運算符'<'沒有爲Iterator基類定義,但運算符'!='是。所以文森特是正確的。 – Jimbo 2013-04-29 07:43:18

回答

3

當你比較操作結束迭代它會編譯!=

+0

: - 你說得對。但在這裏我有一個問題,爲什麼'map'不能和'<'一起工作? – 2013-04-29 07:43:21

+0

增量超過任何迭代器的末端是未定義的行爲,看看http://stackoverflow.com/questions/6673762/why-is-used-with-iterators – chrisw 2013-04-29 07:46:05

+0

它是'地圖'nop'不行,它是''運算符<()'不起作用的'iterator'。 – Axel 2013-04-29 07:46:10