i have a cpp problem and i don\'t know whats wrong.. maybe you can help me :). I\'m trying to implement a data structure for a graph. In this graph i will connect some nodes
One major issue is that your getNodes
function returns a copy of a vector, not the original vector. Therefore your iterators you use in the loops are not iterating over the same vector.
Instead, the iterators you're using in the nested loops are iterating over 4 different (but equivalent) vectors instead of the actual vector from the object in question.
There is nothing wrong in returning a copy of a vector in general. However when you do this, you have to make sure you call such a function if you really want a copy, and not the same vector. Using the getNodes
function as you used it is not a valid usage in terms of what you are trying to accomplish.
The error is here:
inline vector<Node*> getNodes() { return nodes; }
The fix:
inline vector<Node*>& getNodes() { return nodes; }
The latter ensures that a reference to the actual vector in question is returned, not a copy of the actual vector. You can add an additional function that returns the vector as a copy if you want to still have the functionality available.