So I'm recreating a LinkedList in C++ and I am trying to change the pointer to the last Node in my list. Here is my overloaded += operator where all the magic and errors happen. I have two different ways of changing the pointer, but both throw Unhandled exception at 0x00ee42f3 in Lab3.exe: 0xC0000005: Access violation writing location 0xccccccec.. What is going on and how can I fix it?
void MyLinkedList::operator+=(const std::string& s)
{
allocator<Node> a;
Node* n = a.allocate(1);
a.construct(n, s);
if (first == NULL)
first = n;
else
{
(*last).next = n; // crashes here
(*last).SetNext(n); //also will crash here, if the previous statement is removed
last = n;
}
}
To further clarify, it will go through and set the first Node, exit the method, and the next time it is called will run and enter the else statement. At this point there is a second Node, it is allocated in memory and instantiated. What I am trying to do is set the Node* next pointer in the first Node to this new Node, but it throws the exception. Sorry for being very vague initially.