I want to create a priority queue in C++
I created a struct as a general blueprint for instantiating a Priority Queue
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;
}
};
My Coord Class
/*
* 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;
}
};
I attempted to put objects into it. The put statement is causing an error during compile time.
Coord start = Coord(0,0);
PriorityQueue<Coord, double> frontier;
frontier.put(start, 0);
I get an error:
invalid operands to binary expression ('const Coord' and 'const Coord')
Full Error Statement
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)
What does this error even mean? Where do I even begin to debug this error?
For a priority_queue
, the value stored in the queue needs to be sortable. In your case, with a pair
, there is a built in comparison , which you're using with std::greater
. This, in turn, needs all elements of the pair to be ordered. Your Coord
class does not define an operator<
, so you get this error
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.