我正在尝试写一个重载的<运营商,我不断收到此错误
我已经回去并尝试将我的变量dv和函数getDV()更改为const 但这也没有帮助。我觉得我只是在两个不同的错误之间骑自行车。任何帮助都会很棒,你们呀!谢谢!
friend bool operator < (const Vertex & v1, const Vertex & v2);
bool operator < (const Vertex & v1, const Vertex & v2)
{
return v1.getDV() > v2.getDV();
}
///HERE'S MY VERTEX CLASS//////
#include "Vertex.h"
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <fstream>
using namespace std;
const bool myFunction(Vertex & x, Vertex & y);
void shortestPath(vector<Vertex> & v);
///overload the less than operator in order to use the stl sort for vector
///print out the path for each vertex
int main()
{
/////READ ALL THE STUFF INTO THE GRAPH////
ifstream file;
file.open("graph.txt");
cout << "Opening file..." << endl;
if(!file)
{
cout << "System failed to open file.";
}
else
{
cout << "File successfully opened" << endl;
}
int numVertices;
int numEdges;
int num;
int adjacentVertex;
int weight;
file >> numVertices;
vector<Vertex> vertices(numVertices + 1);
for(int i=1;i<=numVertices;i++)
{
file >> numEdges;
Vertex newVertex;
//Using the i counter variable in the outer for loop to identify
//the what vertex what are currently looking at in order to read in the correct adjacent vertex and weight
newVertex.setVertexNum(i);
for(int j=1;j<numEdges;j++)
{
file >> adjacentVertex;
file >> weight;
newVertex.setAdjacentVertex(adjacentVertex, weight);
}
vertices.push_back(newVertex);
}
for(int i=0;i<numVertices+1;i++)
{
cout<< "V" << i <<": ";
for(int j=0;j<vertices[i].getAdjacentVertices().size();j++)
{
cout << "V" << vertices[i].getAdjacentVertices()[j].getAdjVertex() << " " << vertices[i].getAdjacentVertices()[j].getWeight() << endl;
}
}
//CALL THE SHORTEST PATH FUNCTION ON THE GRAPH/////
}
/*
const bool myFunction(const Vertex & x, const Vertex & y)
{
return (x.getDV() >= y.getDV());
}
*/
bool operator < (const Vertex & v1, const Vertex & v2)
{
return v1.getDV() > v2.getDV();
}
void shortestPath(vector<Vertex> & v)
{
sort(v.begin(), v.end());
for(int i=0;i<v.size();i++)
{
cout << v[i].getDV()<< endl;
}
}