涉及std :: equal_range的无效操作数编译器错误 [英] Invalid operand compiler error involving std::equal_range
本文介绍了涉及std :: equal_range的无效操作数编译器错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
对于使用以下模板的间隔图的实现
For an implementation of an interval map using the following template
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()$中的以下代码段c $ c>:
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()实例化
and the following instantiation in 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;}
~~~ ^ ~~~
该错误与以下来源有关代码段(在错误日志中突出显示):
The error pertains to the following source code segment (as highlighted in the error log):
./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 :: pair和以V表示的模板变量(在本例中为char)进行比较而引起的。为避免这种情况,可以使用如下的服装谓词。
error is due to comparing std::pair and template variable denoted by V, in this case char. To avoid this you can use costume predicate as below.
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>());
}
这篇关于涉及std :: equal_range的无效操作数编译器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文