【发布时间】:2016-02-22 04:11:24
【问题描述】:
我正在尝试使用自定义比较函数创建优先级队列,作为类的数据成员。如果我将队列放在一个类中,则代码无法编译,但是如果它在 main 函数中,则它可以正常工作:
#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;
}
我在上面的代码中使用 Microsoft VS2015。我是否将cmp 函数放在类中没有区别。您能否解释一下为什么会发生这种情况以及可能的解决方案?
编辑 1:
main中的这一行
priority_queue<int, vector<int>, decltype(cmp) > pq(cmp); // no error here
确实会产生错误,但我的 IDE 无法检测到它。使用decltype(&cmp) 将消除此错误。
【问题讨论】:
-
decltype(&cmp)工作了吗? (注意添加的运营商地址) -
@DanielJour 它仍然只适用于
main函数,而不是类。
标签: c++ priority-queue