C++ 什么';这个c+有什么问题+;关于申报的代码? 类顶点; 布尔比较Evertex(顶点*v1,顶点*v2); std::优先级队列pq(compareVertex);
上面的代码有什么问题?我已经声明并定义了vertex和compareVertex,但编译器告诉我:“未知类型名‘pq’”和“必须调用对非静态成员函数的引用”C++ 什么';这个c+有什么问题+;关于申报的代码? 类顶点; 布尔比较Evertex(顶点*v1,顶点*v2); std::优先级队列pq(compareVertex);,c++,priority-queue,decltype,C++,Priority Queue,Decltype,上面的代码有什么问题?我已经声明并定义了vertex和compareVertex,但编译器告诉我:“未知类型名‘pq’”和“必须调用对非静态成员函数的引用” 感谢阅读。优先级队列的构造函数中缺少第三个模板参数 参见此以供参考。如文件所述,std::priority_队列模板的签名为: class vertex; bool compareVertex(vertex *v1, vertex *v2); std::priority_queue<vertex *, decltype(compare
感谢阅读。优先级队列的构造函数中缺少第三个模板参数 参见此以供参考。如文件所述,
std::priority_队列
模板的签名为:
class vertex;
bool compareVertex(vertex *v1, vertex *v2);
std::priority_queue<vertex *, decltype(compareVertex)*> pq(compareVertex);
请注意,您的less
专门化必须在全局或std
命名空间中定义
编辑:显然,从注释中可以看出,您的
compareVertex
函数实际上是某个graph
类的成员函数。在这种情况下,您可能希望执行以下操作:
class vertex;
bool compareVertex(vertex *v1, vertex *v2);
namespace std {
template<>
struct less <vertex*> {
bool operator() (vertex* lhs, vertex* rhs) const {
return compareVertex(lhs, rhs);
}
};
}
std::priority_queue<vertex*> pq;
struct VertexComparator{
显式顶点比较器(图形和g)
:图(g){
}
布尔运算符()(顶点*lhs,顶点*rhs)常量{
返回图\uu.compareVertex(lhs,rhs);
}
私人:
图与图;
};
typedef priority_queue tVertexpriority queue;
图形失写;
tVertexPriorityQueue pq(垂直比较器(失写));
bool-graph::compareVal(vertex*v1,vertex*v2){return v1->distdistdist;}上面是我的CompareVertex(名称不同,我的CompareVertex是graph的一个成员函数),我尝试了你告诉我的方式,但它们仍然存在一些问题,当我编写“const”时,编译器告诉我“预期”;“在声明列表的末尾”和“operator()不能是变量或数据成员的名称。”然后我删除了“const”,编译器告诉我“在没有对象参数的情况下调用非静态成员函数”这句话的“return graph::compareVertex(v1,v2);“我被这些错误弄糊涂了,你能帮我弄清楚吗?我的部件上有很多愚蠢的语法错误。它们现在已经修复了,很抱歉。最后一个问题仍然存在:“在没有对象参数的情况下调用非静态成员函数”我的compareVertex是graph的成员函数,所以我用这种方式编写返回”返回图::compareVertex(左、右);“这里出了什么问题???当我将compareVertex设置为一个普通函数时,没有什么问题。正如诊断所暗示的,如果没有对象可以调用图形对象的成员函数,则无法调用该函数。我解决了您在问题中发布的问题,该问题没有说明compare函数是某个类的成员函数。如果问题与您的实际问题不匹配,请编辑它。我知道了,但即使我给它一个容器,我仍然不知道如何初始化它。我尝试了几种方法,但答案仍然是错误的。
class vertex;
bool compareVertex(vertex *v1, vertex *v2);
namespace std {
template<>
struct less <vertex*> {
bool operator() (vertex* lhs, vertex* rhs) const {
return compareVertex(lhs, rhs);
}
};
}
std::priority_queue<vertex*> pq;
struct VertexComparator {
explicit VertexComparator(graph& g)
: graph_(g) {
}
bool operator() (vertex* lhs, vertex* rhs) const {
return graph_.compareVertex(lhs, rhs);
}
private:
graph& graph_;
};
typedef priority_queue<vertex*, vector<vertex*>, VertexComparator> tVertexPriorityQueue;
graph aGraphInstance;
tVertexPriorityQueue pq(VertexComparator(aGraphInstance));