我对 std::less 有一个奇怪的问题。
indexedpriorityq.hpp(21): error C2661: 'std::less<_Ty>::less' : no overloaded function takes 2 arguments
1> with
1> [
1> _Ty=float
1> ]
但这不是它应该做的吗?
这是我的一些代码:
template<class KeyType, class binary_predicate = std::less<KeyType> >
class IndexedPriorityQ
{
private:
typedef typename std::vector<KeyType> KEYLIST;
KEYLIST& m_Keys_V;
[...]
};
template<class KeyType, class binary_predicate>
void IndexedPriorityQ<KeyType, binary_predicate>::
ReorderUpwards(int size)
{
while( (size>1) &&
(binary_predicate(m_Keys_V[m_Heap_V[size]], m_Keys_V[m_Heap_V[size/2]])) //breaks here
)
{
Swap(size/2, size);
size /= 2;
}
}
到底是什么导致了错误,我该如何解决?
最佳答案
std::less
是一个仿函数,它的构造函数 有 0 个参数。也就是说,您可以像这样创建对象:
std::less<Key> a;
然后,你可以像这样使用它:
if(a(x,y)) ...
甚至
if(std::less<Key>()(x,y)) ...
有些仿函数的构造函数接受超过 0 个参数,例如 std::bind1st
。规则是,如果仿函数是二进制的,则它的 operator()
接受 2 个参数。
关于c++ - std::less: error C2661: 'std::less<_Ty>::less': 没有重载函数需要 2 个参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10260430/