Friday, July 31, 2009

C++ - using a vector inside of a Node?

I'm trying to use a vector to keep a list inside of a node...the node class is going to be used in a graph, as each node or point in the graph, and the vector is going to be used as an array of "next" pointers to the other nodes in the graph.





However, I don't really know how to implement this...I have listed this as a value in the Node class:





vector%26lt;pair%26lt;Node%26lt;T%26gt;*,int%26gt; %26gt; paths;





and my constructor for a default node is as follows:





template %26lt;typename T%26gt;


Node%26lt;T%26gt;::Node()


: place(""), value(0), pathdata(NULL, 0), paths(NULL)


{


}





So I want the vector for each node to be called paths, but whenever I try to use paths inside of a method, such as add a path or remove a path (from the vector), I try calling paths.push_back() or paths.erase() and when I compile it, it says "paths not in scope."





How can I fix this?

C++ - using a vector inside of a Node?
I'm not sure about your exact compile error, but I know the VC++ compiler can get very confused when STL containers contain other STL containers. It seems to get confused about mataching up multiple %26lt; %26gt;. The way to get around this is to use spaces when you declare the container of containers. Experiment with adding a space after vector%26lt; (before pair), amd maybe even a space before Node. (Sorry, I'm not at a computer with a compiler to do the experiment myself)
Reply:what's your compiler?





Try to invoke paths via this pointer..





this-%26gt;paths.push_back() ...


No comments:

Post a Comment