【问题标题】:std::less: error C2661: 'std::less<_Ty>::less' : no overloaded function takes 2 argumentsstd::less: 错误 C2661: 'std::less<_Ty>::less' : 没有重载函数需要 2 个参数
【发布时间】:2012-04-21 15:40:05
【问题描述】:

我对 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;
    }
}

究竟是什么导致了错误,我该如何解决?

【问题讨论】:

    标签: c++ templates stl std


    【解决方案1】:

    std::less 是一个仿函数,它的 构造函数 接受 0 个参数。也就是说,您可以像这样创建对象:

    std::less<Key> a;
    

    然后,你可以这样使用它:

    if(a(x,y)) ...
    

    甚至

    if(std::less<Key>()(x,y)) ...
    

    有些函子的构造函数接受多个参数,例如std::bind1st。规则是,如果函子是二进制的,则它的 operator() 接受 2 个参数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-01
      • 2012-04-08
      • 1970-01-01
      • 2018-12-30
      相关资源
      最近更新 更多