Const operators
bool operator==(const Node& node) {
should be
bool operator==(const Node& node) const {
Likewise for long edgeLength(const Node& node1, const Node& node2), edgeExists, printGraph, etc.
Construction
This function:
void createGraph() {
std::cout << "Enter the number of Nodes: ";
std::cin >> count;
for (int i = 0; i < count; i++) {
std::vector<int> v;
node_list.push_back(Node(i + 1));
for (int j = 0; j < count; j++) {
long temp;
std::cin >> temp;
v.push_back(temp);
}
matrix.push_back(v);
}
}
is mostly code that belongs in a constructor. The constructor in this case would accept an istream& and would not cout; that could be done by the caller. The advantage of this approach is that
- it is more flexible - you could deserialize from a file, for example;
- it is more decoupled.
I realize that createGraph is a private which is called by the existing constructor, which is fine; but I would stop short of baking in cout/cin.
Pointer madness
This:
*(&adj_matrix + 1)
scares mewill not do what you want. You're gettingHave you tried executing this method? Based on the address of adj_matrixlink you gave me, which is nowit seems you were attempting to do a triple pointer; assuminghack that the location after it is valid; and dereferencing it. Canrequires that you have a reference to an array with defined size, but you do not just- you only have bare pointers.
Just pass in an integral matrix size?dimensions.
Boolean expressions
if (std::find(edge_list.begin(), edge_list.end(), Edge(node1, node2)) == edge_list.end()) {
return false;
}
return true;
can be
return std::find(edge_list.begin(), edge_list.end(), Edge(node1, node2)) != edge_list.end();