Question
C++ Debugging Help- Graphs I need help figuring out what is wrong with my code. I keep getting an error that says: error: comparison between
C++ Debugging Help- Graphs
I need help figuring out what is wrong with my code. I keep getting an error that says:
error: comparison between signed and unsigned integer expressions [-Werror=sign-compare]
I get this everytime I use the .size() operator. I'm not sure why it is throwing me this error. Here is my code:
#include
#include
#include
#include
#include
#include
#include "Graph.hpp"
using namespace std;
void Graph::addVertex(std::string name){
vertex temp;
temp.name = name;
temp.district = -1;
//temp.distance = 50000000;
temp.visited = false;
vertices.push_back(temp);
}
void Graph::addEdge(std::string v1, std::string v2, int distance){
vertex * tempv1 = findVertex(v1);
vertex * tempv2 = findVertex(v2);
adjVertex adj1;
adj1.distance = distance;
adj1.v = tempv2;
tempv1 -> adj.push_back(adj1);
}
void Graph::displayEdges()
{
for(int i = 0; i < vertices.size(); i++)
{
cout << vertices[i].district << ":" <
for(int j = 0; j < vertices[i].adj.size(); j++)
{
cout << vertices[i].adj[j].v->name;
cout << " (" << vertices[i].adj[j].distance << " miles)";
if (j != vertices[i].adj.size()-1)
cout << "***";
}
cout << endl;
}
}
void Graph::assignDistricts(){
int count = 0;
std::queue
for(int i = 0 ; i < vertices.size() ; i++){
if(vertices[i].district < 0){
count ++;
bft.push(& vertices[i]);
while(!bft.empty()){
vertex *n = bft.front();
n -> district = count;
bft.pop();
for(int x = 0 ; x < n->adj.size() ; x++){
if(n -> adj[x].v -> district < 0){
bft.push(n-> adj[x].v);
}
}
}
}
}
}
vertex* Graph::findVertex(std::string name){
for(int i = 0; i < vertices.size(); i++){
if(vertices[i].name == name)
return &vertices[i];
}
return NULL;
}
void Graph::BFTraversalLabel(std::string startingCity, int distID){
queue
for(int i = 0; i < vertices.size(); i++){
vertices[i].visited = false;
}
for(int i = 0; i < vertices.size(); i++){
if (vertices[i].name == startingCity){
vertices[i].visited = true;
temp.push(&vertices[i]);
vertices[i].district = distID;
break;
}
}
while (temp.empty() != true){
vertex * v1 = temp.front();
temp.pop();
for(int i = 0; i < v1 ->adj.size(); i++){
if (v1 -> adj[i].v -> visited == false){
v1 -> adj[i].v -> visited = true;
v1 -> adj[i].v -> district = distID;
temp.push(v1 -> adj[i].v);
}
}
}
}
--------------------------
Here is the .hpp file
#ifndef GRAPH_HPP
#define GRAPH_HPP
#include
#include
struct vertex;
/*This is the struct for the adjacent vertices for each
vertex in the graph. */
struct adjVertex{
vertex *v;
int distance;
};
/*this is the struct for each vertex in the graph. */
struct vertex{
int ID;
std::string name;
int district;
bool visited;
std::vector
};
class Graph{
public:
Graph();
~Graph();
void addEdge(std::string v1, std::string v2, int distance);
void addVertex(std::string name);
void displayEdges();
void assignDistricts();
private:
std::vector
vertex * findVertex(std::string name);
void BFTraversalLabel(std::string startingCity, int distID);
};
#endif // GRAPH_HPP
Will give thumbs up for correct corrections with good explanation. Thank you
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started