I have trying to implement graph algorithms in C++, but I am getting no instance of overloaded function "std::set<_Key, _Compare, _Alloc>::insert [with _Key=Node &, _Compare=std::less<Node &>, _Alloc=std::allocator<Node &>]" matches the argument list -- argument types are: (Node) -- object type is: std::set<Node &, std::less<Node &>, std::allocator<Node &>>C/C++(304)
every time I use insert
method for a set. Because I am new to C++, I was not able to understand this error despite some Googling and reading documentation.
Is possible to insert a reference into a set and what is the right way of doing so? If not, how should I be doing this instead?
This is a part of the code I have been trying to debug:
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
using namespace std;
class Node {
public:
set<Node &> neighbours;
int value;
Node(int value_) { this->value = value_; }
void addEdgeUndirected(Node &other) {
if (neighbours.count(other) == 0) {
neighbours.insert(other);
other.neighbours.insert(this);
}
}
void print() { cout << value << " "; }
};
class Graph {
map<int, Node &> node_map;
public:
vector<Node &> bfs(Node &node) {
vector<Node &> bfs_traversal; // to store all pointers of all
// the visited nodes
set<Node &> visited;
queue<Node &> to_visit;
to_visit.push(node);
while (!to_visit.empty()) {
Node ¤t = to_visit.front();
to_visit.pop();
visited.insert(current);
for (Node neighbour : current.neighbours) {
if (visited.count(neighbour) == 0) {
to_visit.push(neighbour);
visited.insert(neighbour);
}
}
bfs_traversal.push_back(current);
}
return bfs_traversal;
}
};