2011-12-17 81 views
1

我想創建一個充滿字符串作爲鍵和整數值的映射。當我嘗試使用它進行搜索時,問題就開始了。有人能告訴我我錯了哪裏?我是否在同一個陳述中有兩個地圖?STL映射在搜索嘗試時拋出錯誤

invale is "ale" 
    roomno = 2; 
    // roomlist is a map 
    // rinventory is another map 

    if(roomlist[roomno].rinventory.find(invale) != map<string, int>::end()); 

我得到的錯誤如下。什麼重載功能?這確實是一個漫長的錯誤。

error C2668: 'std::_Tree<_Traits>::end' : ambiguous call to overloaded function 
1>  with 
1>  [ 
1>   _Traits=std::_Tmap_traits<std::string,int,std::less<std::string>,std::allocator<std::pair<const std::string,int>>,false> 
1>  ] 
1>  c:\program files\microsoft visual studio 9.0\vc\include\xtree(569): could be 'std::_Tree<_Traits>::const_iterator std::_Tree<_Traits>::end(void) const' 
1>  with 
1>  [ 
1>   _Traits=std::_Tmap_traits<std::string,int,std::less<std::string>,std::allocator<std::pair<const std::string,int>>,false> 
1>  ] 
1>  c:\program files\microsoft visual studio 9.0\vc\include\xtree(564): or  'std::_Tree<_Traits>::iterator std::_Tree<_Traits>::end(void)' 
1>  with 
1>  [ 
1>   _Traits=std::_Tmap_traits<std::string,int,std::less<std::string>,std::allocator<std::pair<const std::string,int>>,false> 
1>  ] 
1>  while trying to match the argument list '(void)'  

在此先感謝您。

回答

3

嘗試將其更改爲:

if(roomlist[roomno].rinventory.find(invale) != roomlist[roomno].rinventory.end()); 
+0

謝謝您的幫助 – 2011-12-17 09:06:03

0

應該

if(roomlist[roomno].rinventory.find(invale) != roomlist[roomno].rinventory.end()); 
0

map::end()方法也不是一成不變的。

正確的方法是這樣的:

map<string, int>::iterator it = roomlist[roomno].rinventory.find(invale); 
if(it != roomlist[roomno].rinventory.end()) 
    // do stuff 
相關問題