This code is giving me runtime error. Here, first I want to take a integer input which determine the number (n) of nodes in the graph. Then for n-1 time I want take two integer input which will give the adjacency information.
... creates a pointer (to memory) but you fail to provide any memory to point to. You can do one of three things: Create an object instead of a pointer ( ... ) Create an object and assign the pointer to that objects address ( ... ) Allocate the memory yourself ( ... )
graph *g; creates a pointer (to memory) but you fail to provide any memory to point to. You can do one of three things:
Create an object instead of a pointer ( graph g; )
Create an object and assign the pointer to that objects address ( graph g, *gp = &g; )
Allocate the memory yourself ( graph * g = new graph; )
Thank You. 2nd one is working for me. But what about 1st one? How can I assign value for the object(g) using 1st option?
Would you plz make me clear about how 2nd one is working? Is it like this... For example, pointer gp is first pointing the object g which is a graph type object. then the value of total_nodes is assigned through the pointer to the object.
When you have syntax like g->total_nodes it means that you access the object pointed to by "g" and then access its data member called total_nodes .
When you write it as g.total_nodes it means that you access the data member called total_nodes that belongs to the object "g".
So, if you want the first version to work, i.e. the version with Graph g; , then you have to replace all the g-> with g. , because now, "g" is an object, and not a pointer to an object (like in your original code).