0

I recently switched from java to c++ and am not too familiar with the functions of different c++ data structures. The code below has a queue, Q, of Pets. I am trying to sort the queue by using the struct cmp.

#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
enum owner { A, B };

class Pet {
    public:
        int b, e, id;
        owner o;
};

struct cmp
{
    bool operator()(Pet a, Pet b) const
    {
        return a.id - b.id;
    }
};

int main(){
    queue<Pet, cmp> Q;
}

However, the compiler is giving this error:

no type named 'value_type' in 'cmp'

I have tried debugging, looking at other posts, and changing the (Pet a, Pet b) in cmp to (int a, int b) & etc.

codexistent
  • 128
  • 11
  • 2
    Two problems: 1) When you subtract two values, in either Java or C++, the return value is not a `bool`. 2) The 2nd parameter to the `std::queue` is not a comparator class. Queues do not have comparator classes, it makes no sense for them to have one. Which part of your C++ textbook's description of `std::queue` led you to believe that? – Sam Varshavchik Feb 01 '23 at 16:31
  • 2
    several hours of guessing can save you from 5 mintues of reading documentation https://en.cppreference.com/w/cpp/container/queue. – 463035818_is_not_an_ai Feb 01 '23 at 16:33
  • 1
    a queue by itself does not care how you compare its elements – 463035818_is_not_an_ai Feb 01 '23 at 16:36
  • if you want to sort elements then most likely `std::queue` is the wrong container, it models a FIFO, its not meant to be sorted by you – 463035818_is_not_an_ai Feb 01 '23 at 16:38
  • There is, actually, a queue-like container for which a comparator class makes sense (presuming that the actual comparator logic here gets fixed up). Continuing to read the C++ textbook or reference material should, at some point, get around to it. – Sam Varshavchik Feb 01 '23 at 16:40
  • Probably you are not only not familiar with the, for you, new C++ libraries. But be you will also be making assumptions on how C++ works based on your experience with java, and they will be wrong. My advice first learn a bit about how C++ really works (for example : life cycle of objects in C++ are managed by scope) before blindly copying code from java to C++. – Pepijn Kramer Feb 01 '23 at 16:42
  • Side note : `using namespace std;` is not recommended, just learn to type `std::` where needed (it will prevent name clashes with other code later) – Pepijn Kramer Feb 01 '23 at 16:43
  • For a queue with sort order there is [`std::priority_queue`](https://en.cppreference.com/w/cpp/container/priority_queue). Note for C++ developers, cppreference.com is one of the best sites to lookup information. And there are C++ [books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) or https://www.learncpp.com/ to learn more about C++ in general. – Pepijn Kramer Feb 01 '23 at 16:44
  • 1
    To top things off, your ordering function is not valid for any of the standard ordered containers. Get a [good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and start with chapter one. You must unlearn Java. – molbdnilo Feb 01 '23 at 16:56
  • got it, thanks for all the insights – codexistent Feb 01 '23 at 17:05

0 Answers0