我刚刚开始学习C++,有一半时间我不知道自己在做什么,花几个小时在Google上搜索并盲目地将代码放入我的项目中,这可能是一个基本问题,但我似乎无法忍受把它弄好.
这是我的任务要求,我需要这些:
在Edge类中:
public:
bool operator()(Edge*, Edge*)
Run Code Online (Sandbox Code Playgroud)
在Graph类中:
private:
priority_queue<Edge*, vector<Edge*>, Edge> edges
Run Code Online (Sandbox Code Playgroud)
我有问题声明priority_queue.细节:
如果我直接使用这些,边类会给我一个错误"必须有一个类的参数",我明白我不能将两个指针重载到bool运算符中,所以这就是我试过的:
在Edge.cpp中:
#include "Edge.h"
using namespace std;
Edge::Edge(Vertex* s, Vertex* d, double w)
{
source = s;
destination = d;
weight = w;
}
double Edge::getWeight()
{
return weight;
}
struct CompareWeight : public std::binary_function<Edge*, Edge*, bool>
{
bool operator()(Edge* e1,Edge* e2)
{
double w1 = e1->getWeight();
double w2 = e2->getWeight();
if(w1>w2){
return true;
}
else {
return false;
}
}
}; …Run Code Online (Sandbox Code Playgroud)