I've got several problems with my adjacency graph in C++. I fixed some of the main errors, but still can run and test the program. I'm not sure if the newEdge()
method is working properly, if the vector in the vector is right created, and most importantly how to display the edges in the graph.
There is the code and I marked the lines with errors with "!":
#include <iostream>
#include <algorithm>
#include <fstream>
#include <vector>
using namespace std;
struct Edge
{
int begin;
int end;
};
class Graph
{
private:
int numOfNodes;
vector<vector<Edge>> baseVec;
public:
Graph(int numOfNodes)
{
baseVec.resize(baseVec.size() + numOfNodes);
}
void newEdge(Edge edge)
{
if (edge.begin >= numOfNodes-1 || edge.end >= numOfNodes-1 || edge.begin < 0 || edge.end < 0)
{
cout << "Invalid edge!\n";
}
baseVec[edge.begin].emplace_back(edge.end); !!
baseVec[edge.end].emplace_back(edge.begin); !!
}
void display()
{
for (int i = 0; i < baseVec.size(); i++)
{
cout << "\n Adjacency list of vertex " << i << "\n head "; !!!
for (int j = 0; j < baseVec[i].size(); j++) !!!
{
cout << baseVec[i][j] << " "; !!!!!!!
cout << endl;
}
}
}
};
std::ostream &operator<<(std::ostream &os, Edge const &m)
{
return os << m.begin << m.end;
}
int main()
{
int vertex, numberOfEdges, begin, end;
cout << "Enter number of nodes: ";
cin >> vertex;
numberOfEdges = vertex * (vertex - 1);
Edge edge;
Graph g1(vertex);
for (int i = 0; i < numberOfEdges; i++)
{
cout << "Enter edge ex.1 2 (-1 -1 to exit): \n";
cin >> edge.begin >> edge.end;
if ((begin == -1) && (end == -1))
{
break;
}
g1.newEdge(edge);
}
g1.display();
return 0;
}
I overloaded the << operator, not sure if it's right and the two errors I have in Visual Studio are:
'<': signed/unsigned mismatch
binary '<<': no operator found which takes a right-hand operand of type '_Ty' (or there is no acceptable conversion)
This is a new version of my prior question.