简体   繁体   中英

C++ priority_queue doesn't have push?

I have these at the top of my file:

#include <queue>

typedef struct cell_s {
    unsigned int x;
    unsigned int y;
    unsigned int ambiguity = 9;
} cell_t;

// so the priority_queue can sort the structs:
bool operator<(const cell_t &a, const cell_t &b) {
    return a.ambiguity < b.ambiguity;
}

In the same file I have declared a priority_queue as a private member of a class, like this:

priority_queue<cell_t> blankCellQueue;

Then I have a member function of the same class that contains these lines:

cell_t cell;
cell.x = x;
cell.y = y;
blankCellQueue.push(cell);

But Xcode is presenting this warning on the last line I shared:

"No matching member function for call to 'push'".

priority_queue is part of the namespace, std . You need to use std::priority_queue .

C++11's std::priority_queue is a standard container adapter and it does have member function push() - see the linked reference.

It is possible, though, that your implementation of the C++ Standard Library is not compliant. You should upgrade to the latest version.

You can see your code compiling here . It required adding the std:: namespace qualifier before priority_queue , but I do not believe that to be the root cause of the issue here. If it were, your compiler error should be different (or you should receive other errors before).

Sorry to have wasted all of your time. I found the issue. The priority_queue is a member of my class. The function I was attempting to edit the priority_queue from was a member function that I had marked as const, because I hadn't anticipated a need to modify any class variables when I originally wrote it without any queue. Invoking push on a member object from a const member function is an obvious no-no. I just wish Xcode had given a slightly useful error messages... something along the lines of "non const function invoked from const function" would be a very helpful message.

Since I hadn't provided the member function declaration, it would have been very difficult for anyone to have pointed that out to me here.

Thanks for the offers of help!

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM