My quest continues ! I started using a list for my priority queue
std::priority_queue<charElement,std::list<charElement>,compareCharElt> Q_freqDistribution;
where charElement does not (as of this erroneous compilation) overload the ‘-‘ operator
When I got this error :
std::_List_unchecked_iterator<_Mylist>' does not define this operator or a conversion to a type acceptable to the predefined operator *
I looked it up and from what I understood , it seemed that in order for me to be able to use a list (I assume for the iterator to be able to traverse elements) I needed to overload the ‘-‘ operator , so I did , here’s the method header :
void operator-(charElement&);
At this point I just wanted a dummy operator to make sure what I understood was correct, sadly , I am wrong , I’ve been searching for a while about this now and I cant seem to be able to pin point the problem, can anyone help ?
The underlying container for a
priority_queuemust be a sequence container with random access iterators (andfront(),push_back()andpop_back()operations).listhas bidirectional iterators, not random access iterators, so you can’t use it here.You can use
vector(the default),deque, or any non-standard container that meets these requirements.You shouldn’t need to overload
operator-for your type; the compiler is complaining thatoperator-is not overloaded forlist::iterator. The only requirement on the type is that it can be compared with other values of that type; in your case, you’re providing a custom comparator for that.