Pregunta

I am trying to implement heapsort in C++ that would take a structure of two integers in it and sort them in a lexicographical order. However, Visual Studio throws at me a bunch of errors, like

Error   8   error C2784: 'bool std::operator <(const std::_Revranit<_RanIt,_Base> &,const std::_Revranit<_RanIt2,_Base2> &)' : could not deduce template argument for 'const std::_Revranit<_RanIt,_Base> &' from 'const lol'

I don't really knows what that means, but I think something wrong is in the very declaration. I did this:

struct lol {
    int num1;
    int num2;
};

And I declared the priority_queue like this:

priority_queue<lol> q;
¿Fue útil?

Solución

The type lol needs an operator< function so that the priority_queue knows how to sort them. The compiler doesn't provide one by default. There are a few options.

To get the code to compile without changing the declaration of priority_queue<lol> q; you will need to provide the operator. You can declare it as a free function. Here's an example; assuming you would sort a lol struct based on the num1 field (not sure how you actually want to do it).

bool operator< (const lol &left, const lol &right) {
    return left.num1 < right.num1;
}

Note, if num1 was private, you'd need to make the operator a friend of lol.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top