[英]Dijkstras algorithm with priority_queue
I am trying to implement Dijkstra's algorithm. 我正在尝试实现Dijkstra的算法。 I am using this priority_queue 我正在使用这个priority_queue
priority_queue<pair<PathInfo,string>,vector<pair<PathInfo,string> >,QueueComp> p;
where 哪里
class QueueComp{
PathComp* pc;
public:
QueueComp(PathComp*);
bool operator ()(const pair<PathInfo,string>&,const pair<PathInfo,string>& );
};
is my "Compare" function. 是我的“比较”功能。 The error is that QueueComp does not have a default constructor and I am not permitted to create one . 错误是QueueComp 没有默认的构造函数,并且不允许我创建一个 。 What can I do to make my code compile? 我该怎么做才能编译代码? Btw this is the error 顺便说一句这是错误
error: no matching function for call to 'QueueComp::QueueComp()'
This is the pathcomp.h 这是pathcomp.h
class PathComp{
public:
virtual bool betterThan(const PathInfo& path1,const PathInfo& path2)=0;
};
This is the pathcomppl.h 这是pathcomppl.h
#include "pathcomp.h"
class PathCompPL:public PathComp{
public:
virtual bool betterThan(const PathInfo& path1,const PathInfo& path2);
};
This is the pathcomppl.cpp 这是路径comppl.cpp
#include "pathcomppl.h"
bool PathCompPL::betterThan(const PathInfo& path1,const PathInfo& path2){
if (path1.getTotalPrice()>path2.getTotalPrice())
return true;
if (path1.getTotalPrice()==path2.getTotalPrice() && path1.getTotalLength()>path2.getTotalLength())
return true;
return false;
}
Expanded error message 扩展错误信息
main.cpp: In constructor ‘std::priority_queue<_Tp, _Sequence, _Compare>::priority_queue(const _Compare&, const _Sequence&) [with _Tp = std::pair<PathInfo, std::basic_string<char> >; _Sequence = std::vector<std::pair<PathInfo, std::basic_string<char> > >; _Compare = QueueComp]’:
main.cpp:11:87: error: no matching function for call to ‘QueueComp::QueueComp()’
main.cpp:11:87: note: candidates are:
In file included from main.cpp:5:0:
queuecomp.h:14:5: note: QueueComp::QueueComp(PathComp*)
queuecomp.h:14:5: note: candidate expects 1 argument, 0 provided
queuecomp.h:10:7: note: QueueComp::QueueComp(const QueueComp&)
queuecomp.h:10:7: note: candidate expects 1 argument, 0 provided
You need to initialize your priority queue with additional parameter since you have non-default constructor. 由于您具有非默认构造函数,因此需要使用其他参数初始化优先级队列。
priority_queue<pair<PathInfo,string>,vector<pair<PathInfo,string> >,QueueComp> p(QueueComp(ptrToPathCompObject));
The additional parameter ( QueueComp(ptrToPathCompObject)
) should fix your problem. 附加参数( QueueComp(ptrToPathCompObject)
)应该可以解决您的问题。
I am assuming that you have already implemented the operator()
in QueueComp class. 我假设您已经在QueueComp类中实现了operator()
。
You do not have a default constructor, because you are supposed to initialize the variable called pc. 您没有默认的构造函数,因为应该初始化名为pc的变量。 You have this constructor: 您具有以下构造函数:
QueueComp(PathComp*);
You have to implement it so that pc is associated to the parameter. 您必须实现它,以便pc与参数关联。
As about your second question: The first element is your next priority, the second element is the set of lower priorities and the third is the queue comparison. 关于第二个问题:第一个元素是您的下一个优先级,第二个元素是较低优先级的集合,第三个是队列比较。 I hope this helps you. 我希望这可以帮助你。
It looks like your issue is with implementing a proper comparator. 看来您的问题在于实现适当的比较器。 One alternative you might consider is to create the comparator like the following 您可能考虑的一种替代方法是创建比较器,如下所示
struct CompareEdgeWeights : public binary_function<PathInfo*, PathInfo*, bool>
{
bool operator()(const PathInfo* left, const PathInfo* right) const
{
return left->getEdgeWeight() > right->getEdgeWeight();
}
}; // end struct
// Priority queue of node edges
priority_queue<PathInfo*,vector<PathInfo*>,CompareEdgeWeights > * edgePriorityQueue;
Have this struct inherit from binary_function and overload the operator (). 让此结构继承binary_function并重载operator()。 You can then use this as your comparator for keeping the edges sorted from lowest to highest weight value. 然后,您可以将其用作比较器,以保持边缘的权重值从最低到最高排序。 Note: you may have to tweak this a bit to conform to your implementation. 注意:您可能需要稍微调整一下以符合您的实现。 It is difficult to give a 100% correct suggestion without seeing more of your implementation. 如果不看更多实施,很难给出100%正确的建议。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.