【问题标题】:Invalid operand compiler error involving std::equal_range涉及 std::equal_range 的无效操作数编译器错误
【发布时间】:2018-02-21 01:46:28
【问题描述】:

对于使用以下模板的区间图的实现

template<class K, class V>
intervalMap{
public:
intervalMap(V const&);
public:
void assign(K const&, K const&, V const&);
private:
std::map<K,V>m_map;
};

以及成员函数assign()内的如下代码段:

template<class K, class V> void intervalMap::assign(
                                             K const& keyBegin, 
                                             K const& keyEnd,
                                             V const& val
                                            ){

            auto begin = m_map.find(keyBegin);
            auto end = m_map.find(keyEnd);

            auto p = std::equal_range(begin,end,val);

}

以及以下在 main() 中的实例化

intervalMap<unsigned int, char> iMap('A');
iMap.assign(1,2,'A');

以下编译错误结果:

In file included from main.cpp:1:
In file included from ./code.hpp:4:
In file included from /Library/Developer/CommandLineTools/usr/
include/c++/v1/map:442:
In file included from /Library/Developer/CommandLineTools/usr
/include/c++/v1/__tree:18:
/Library/Developer/CommandLineTools/usr/include/
c++/v1/algorithm:701:71: 
error: invalid operands to binary expression 
('const std::__1::pair<const unsigned int, char>' and 'int')
    bool operator()(const _T1& __x, const _T2& __y) 
const {return __x < __y;}
              ~~~ ^ ~~~

错误与以下源代码段有关(在错误日志中突出显示):

./code.hpp:120:18: note: in instantiation of function 
template specialization 'std::__1::equal_range
<std::__1::__map_iterator
<std::__1::__tree_iterator
<std::__1::__value_type
<unsigned int, char>, 
std::__1::__tree_node
<std::__1::__value_type
<unsigned int, char>, 
void *> *, long> >, 
char>' 
requested here
        auto p = equal_range(begin,end,val);
                 ^
./code.hpp:205:10: note: 
in instantiation of member function 
'interval_map<unsigned int, char>::assign' 
requested here
    iMap.assign(1,2,'A');

欣赏建议。

【问题讨论】:

  • std::map m_map; 此行无效。
  • 抱歉打错了...将编辑我的原始帖子
  • 当您取消引用beginend 时,您认为您会得到什么类型?这是否解释了错误消息?
  • @Praetorian 解除引用 begin 或 end 应该获取一个 std::pair 对应于地图的特定键索引。但是,当我使用正确的参数调用 std::equal_range() 时,为什么会发生此错误?
  • 因为equal_range 将尝试比较pair&lt;const K, V&gt;char。您需要使用带有自定义比较器的重载。

标签: c++ compiler-errors c++14


【解决方案1】:

错误是由于比较了 std::pair 和 V 表示的模板变量,在本例中为 char。为避免这种情况,您可以使用以下服装谓词。

template<class K, class V>
class intervalMap{
public:
intervalMap(V  val)
{
   v = val;
}
public:
void assign(K const a, const K  b,  const V c);

private:
V v;
std::map<K,V> m_map;

};
template<class K, class V>
struct comp
{
    bool operator()(std::pair<K, V> a, V b)
    {
        return a.second < b;
    }
    bool operator()(V a, std::pair<K, V> b)
    {
        return a < b.second;
    }
};

template<class K, class V>
void intervalMap<K, V>::assign(const K  keyBegin, const K  keyEnd, const V  
val)
{
    auto begin = m_map.find(keyBegin);
    auto end = m_map.find(keyEnd);


    auto p = std::equal_range(begin, end, val,comp< K,  V>());
}

【讨论】:

    猜你喜欢
    • 2017-12-20
    • 2012-03-02
    • 1970-01-01
    • 1970-01-01
    • 2022-11-13
    • 2012-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多