将对象放入C ++中的Priority Queue中会导致无效的操作数转换为二进制表达式

时间:2019-02-17 01:13:29

标签: c++ struct

我想用C ++创建优先级队列

我创建了一个作为实例化优先级队列的一般蓝图的结构

template<typename T, typename priority_t>
struct PriorityQueue {
  typedef std::pair<priority_t, T> PQElement;
  std::priority_queue<PQElement, std::vector<PQElement>,
                 std::greater<PQElement>> elements;

  inline bool empty() const {
     return elements.empty();
  }

  inline void put(T item, priority_t priority) {
    elements.emplace(priority, item);
  }

  T get() {
    T best_item = elements.top().second;
    elements.pop();
    return best_item;
  }
};

我的协调员班

/*
 * Structure for a coordinate
 */
class Coord{ 
    public:
        int x, y;

    public:
        Coord(int x1, int y1) {
            x = x1;
            y = y1;
        }
        int getX(void) {
            return x;
        }
        int getY(void) {
            return y;
        }
};

我试图将对象放入其中。 put语句在编译期间导致错误。

Coord start = Coord(0,0);
PriorityQueue<Coord, double> frontier;
frontier.put(start, 0);

我收到一个错误:

invalid operands to binary expression ('const Coord' and 'const Coord')

完整错误声明

error: invalid operands to binary
      expression ('const Key' and 'const Key')
    return __x.first < __y.first || (!(__y.first < __x.first) && __x.second < __y.second);
                                                                 ~~~~~~~~~~ ^ ~~~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/utility:582:16: note: in instantiation of function
      template specialization 'std::__1::operator<<double, Key>' requested here
    return __y < __x;
               ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/functional:724:21: note: in instantiation of function
      template specialization 'std::__1::operator><double, Key>' requested here
        {return __x > __y;}
                    ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/algorithm:4973:13: note: in instantiation of member
      function 'std::__1::greater<std::__1::pair<double, Key> >::operator()' requested here
        if (__comp(*__ptr, *--__last))
            ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/algorithm:5001:5: note: in instantiation of function
      template specialization
      'std::__1::__sift_up<std::__1::greater<std::__1::pair<double, Key> > &, std::__1::__wrap_iter<std::__1::pair<double, Key> *> >' requested here
    __sift_up<_Comp_ref>(__first, __last, __comp, __last - __first);
    ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/queue:690:12: note: in instantiation of function template
      specialization 'std::__1::push_heap<std::__1::__wrap_iter<std::__1::pair<double, Key> *>, std::__1::greater<std::__1::pair<double, Key> > >' requested
      here
    _VSTD::push_heap(c.begin(), c.end(), comp);
           ^
lab1.cpp:87:14: note: in instantiation of function template specialization 'std::__1::priority_queue<std::__1::pair<double, Key>,
      std::__1::vector<std::__1::pair<double, Key>, std::__1::allocator<std::__1::pair<double, Key> > >, std::__1::greater<std::__1::pair<double, Key> >
      >::emplace<double &, Key &>' requested here
    elements.emplace(priority, item);
             ^
lab1.cpp:305:14: note: in instantiation of member function 'PriorityQueue<Key, double>::put' requested here
    frontier.put(start, 0);
             ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/utility:572:1: note: candidate template ignored: could not
      match 'pair<type-parameter-0-0, type-parameter-0-1>' against 'const Key'
operator< (const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y)

此错误甚至意味着什么?我什至从哪里开始调试此错误?

1 个答案:

答案 0 :(得分:0)

对于priority_queue,存储在队列中的值需要可排序。在您的情况下,对于pair,有一个built in comparison,您正在与std::greater一起使用。反过来,这需要对中的 all 个元素进行排序。您的Coord类未定义operator<,因此您会收到此错误