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; )
L7Sqr
Practically a Posting Shark
866 posts since Feb 2011
Reputation Points: 253
Solved Threads: 155
Skill Endorsements: 7
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).
mike_2000_17
21st Century Viking
3,166 posts since Jul 2010
Reputation Points: 2,082
Solved Threads: 636
Skill Endorsements: 42
Question Answered as of 1 Year Ago by
Ali_2101,
mike_2000_17
and
L7Sqr