【发布时间】: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;此行无效。 -
抱歉打错了...将编辑我的原始帖子
-
当您取消引用
begin或end时,您认为您会得到什么类型?这是否解释了错误消息? -
@Praetorian 解除引用 begin 或 end 应该获取一个 std::pair 对应于地图的特定键索引。但是,当我使用正确的参数调用 std::equal_range() 时,为什么会发生此错误?
-
因为
equal_range将尝试比较pair<const K, V>和char。您需要使用带有自定义比较器的重载。
标签: c++ compiler-errors c++14