I have a class network which contains a std::set of nodes. Here is a snip of the network class
1 2 3 4 5 6
|
class network{
protected:
std::set<node >m_nodes;
public:
void setupNetwork();
};
| |
SetupNetwork is some code which generates a list of locations for the nodes, and then builds a set of relationships between the nodes, before inserting them into the set. This all works perfectly. The issue is that certain nodes are neighbours to each other, and I would like for the nodes to be able to store a list of pointers to their neighbour nodes.
1 2 3 4 5 6
|
class node{
private:
std::set<node *> m_neighbours;
public:
void addNeighbour(node *);
};
| |
This is not inherently an issue, except that the way in which I establish who the neighbours are is by attempting an insertion into the first set (network::m_nodes), and then looking at the returned pair. Node Equivalence is determined by the x and y values of the node, so this insertion gives me either a matching node, or adds it and gives me the actual node.
1 2 3 4 5 6 7 8 9
|
std::pair<std::set<node>::iterator, bool> curNode;
curNode = m_nodes.insert(xVal, yVal);
//for each neighbour
do{
std::pair<std::set<node>::iterator, bool> curNeighbour;
curNeighbour = m_nodes.insert(node(xVal, yVal);
//Where xVal & yVal come from is unimportant
(*curNode.first).addNeighbour(&(*curNeighbour.first));
}while(There are still neighbours);
| |
Now we get to the crux of it. I can't do the call on line 8 because addNeighbour is not const (it can't be since it involves an insertion into another set node::m_neighbours), but &(*curNeighbour.first) is a const node *.
I thought that maybe I could let m_neighbour become a std::set<std::set<node >::iterator > instead of a pointer, i.e.
1 2 3 4 5 6
|
class node{
private:
std::set<std::set<node>::iterator > m_neighbours;
public:
void addNeighbour(std::set<node >::iterator);
};
| |
Since I could easily access each neighbour. This, however causes the same problem.
Can I handle this issue wihtout having to make network::m_nodes a set of node *'s?