【问题标题】:STL Map throwing an error upon search attemptSTL Map 在搜索尝试时抛出错误
【发布时间】:2011-12-17 08:40:24
【问题描述】:

我正在尝试创建一个充满字符串作为键和整数作为值的映射。当我尝试使用它进行搜索时,问题就开始了。有人能告诉我哪里出错了吗?是不是我在同一个语句中有两个地图?

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

提前谢谢你。

【问题讨论】:

    标签: c++ string stl maps


    【解决方案1】:

    尝试将其更改为:

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

    【讨论】:

      【解决方案2】:

      map::end() 方法不是静态的。

      正确的做法是这样的:

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

      【讨论】:

        【解决方案3】:

        应该是

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-03-04
          • 1970-01-01
          • 2013-04-11
          • 1970-01-01
          • 2017-01-12
          相关资源
          最近更新 更多