I am trying to make a priority queue with a custom compare function, as a data member of a class. The code fails to compile if I put the queue inside a class, however it works fine if it is inside the main function:
#include <queue>
#include <vector>
using namespace std;
bool cmp(int x, int y) { return (x > y); }
class A {
public:
private:
priority_queue<int, vector<int>, decltype(cmp) > pq(cmp); // Error at pq(cmp) : function "cmp" is not a type name
};
int main() {
priority_queue<int, vector<int>, decltype(cmp) > pq(cmp); // no error here
return 0;
}
I am using Microsoft VS2015 for the above code. It makes no difference whether I put the cmp function inside the class. Could you explain why this happens and a possible solution for this?
Edit 1:
This line in main
priority_queue<int, vector<int>, decltype(cmp) > pq(cmp); // no error here
does produce an error, but my IDE is not able to detect it. Use decltype(&cmp) will eliminate this error.
decltype(&cmp)working? (Note the added address of operator)mainfunction, not the class.