I am at the early stages of creating a VERY simple graph. The code below reads commands from a text file such as (for testing purposes):
1 2 3
add a g
add d t
add o p
However, when it pushes back vp1 into the vector near the end of the while loop at line 39, it doesn't seem to keep the value/pointer that exists right before it pushes it, as the small debug messages that I've inserted show. Specifically, the for-loop that goes through the vector prints out the same garbage value. But when I simply push back the test value vp2 from line 20 outside of the while loop, it prints that value out properly but none of the others.
If a function returns a pointer to a local, the caller cannot safely dereference the pointer because the pointed-to object was destructed when the function returned. Dynamically allocate an object with new and return the pointer, or change vertices to std::vector<vertex> (no pointers).
By the way, I'm not sure what you're trying to do, but at first glance the vertex class looks ill-defined to me.
The pointer returned from createVertex is a pointer to a local variable that stops existing as soon as the function returns, so you cannot dereference the returned pointer without evoking undefined behavior, since it doesn't point to a valid object.
Is there a reason you can't store vertex objects directly?
ugh I reverted back to storing vertex objects directly because I have non idea how I ended up using pointers lol. Definitley much better! Just had a total brain crap-out. Thanks for the help guys! I really appreciate it. And yes, data structure is a mess right now but I'm at the early stages of testing all of this out. Still sonewhat new.