Thursday, 12 September 2013

C++ Linked List Access Violation

C++ Linked List Access Violation

I've tried searching extensively, but all related answers seem to suggest
there is a null pointer somewhere causing trouble. I've checked several
times and it is possible I'm just blatantly missing it. However since the
error will always occur at the end of the node::node(const myObject&
newmyObject) function no matter how much gibberish I put after the
myObject data = newmyObject;, and before returning to tail = head; I am
honestly at a loss.
This is a first year programming task and neither the lectures nor the
textbook go into any detail on a linked list involving objects, so any
direction would be appreciated.
Exact error when using Visual Studio debugger: First-chance exception at
0x00E38FFB in Assignment1.exe: 0xC0000005: Access violation writing
location 0xCCCCCCCC.
node.h
class node
{
public:
node* next;
myObject data();
node();
node(const myObject& newmyObject);
private:
};
node.cpp
node::node()
{
next = NULL;
}
node::node(const myObject& newmyObject)
{
next = NULL;
myObject data = newmyObject;
} // << crashes upon reaching the end of this, statements between
newmyObject and here will execute fine
LinkedList.h
class LinkedList
{
public:
LinkedList();
void addmyObject(myObject* newmyObject);
private:
int size;
node* head;
node* tail;
};
LinkedList.cpp
LinkedList::LinkedList()
{
node* head = NULL;
node* tail = NULL;
myObject* tempmyObject;
}
void LinkedList::addmyObject(myObject* newmyObject)
{
myObject * tempmyObject = newmyObject;
if (head == NULL)
{
head = new node(*tempmyObject);
tail = head;
}
else
{
node* newNode = new node(*tempmyObject);
tail->next = newNode;
tail = newNode;
}
}

No comments:

Post a Comment