Here is the copy constructor.
Code:
Stack::Stack(const Stack& a_stack)
{
if (a_stack.top != NULL)
{
StackFramePtr temp = a_stack.top;
StackFramePtr end = new StackFrame;
end->data = temp->data;
top = end;
temp = temp->link;
while (temp != NULL)
{
end = end->link;
end = new StackFrame;
end->data = temp->data;
temp = temp->link;
}
end->link = NULL;
}
else top = NULL;
}
The problem lies within these lines..
Code:
end = end->link;
end = new StackFrame;
The correct way to do this is given in the example..
Code:
end->link = new StackFrame;
end = end->link;
To me it seems like these two pairs of lines do the exact same thing, but in a different order. The order does not seem like it should make a difference but it does. When the first pair of lines is used in the copy constructor, it seems like it copies the first object from the stack that is being copied, then it goes on to create an infinite stack where each StackFrame object's data variable is assigned to random garbage. I do not believe this is some sort of loose pointer problem. Thanks for any help this is just something that I can't figure out on my own.