Unhandled exception error
Oct 22, 2014 at 7:13pm UTC
I am having trouble running my code. It all compiles, but when i run it u get this error
"Unhandled exception at 0x001f18ac in Project 2.exe: 0xC0000005: Access violation reading location 0x00000004."
If anyone knows how to fix this I would greatly appreciate it
This must be done using a linked list stack
This is my header file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
template <class DataType>
struct Node{
DataType info;
Node<DataType>*next;
};
template <class DataType>
class Stack
{
public :
Stack();
void push(DataType elementToPush);
bool pop(DataType & poppedElement);
Stack(const Stack<DataType> &element); // Copy constructor
~Stack(); // Destructor
Stack<DataType> & operator =(const Stack<DataType> &element); //Overload assignment operator
bool isEmpty()const ;
void makeEmpty();
private :
Node<DataType>*top;
inline void deepCopy(const Stack<DataType> & original);
};
template <class DataType>
Stack<DataType>::Stack()
{
top=NULL;
}
template <class DataType> // Remove the node at the front of the list and return the element
bool Stack<DataType>::pop(DataType & element)
{
if (isEmpty()==true )
return false ;
else
{
Node<DataType>*ptr=top->next;
top->next=ptr->next;
element=ptr->info;
delete ptr;
return true ;
}
}
template <class DataType> // Make a new node for the element and push it to the front of the list
void Stack<DataType>::push(DataType element)
{
Node<DataType>*ptr=new Node<DataType>;
ptr->info=element;
ptr->next=top->next;
top->next=ptr;
}
template <class DataType> // Check to see if the list is empty
bool Stack<DataType>::isEmpty()const
{
return top==NULL;
}
template <class DataType> // Empty the list out
void Stack<DataType>::makeEmpty()
{
while (top->next != NULL)
{
Node<DataType>*ptr=top->next;
top->next=ptr->next;
delete ptr;
}
}
template <class DataType> // Deep copy
inline void Stack<DataType>::deepCopy(const Stack<DataType> & original)
{
Node<DataType>*copyptr=new Node<DataType>;
Node<DataType>*newtop=copyptr;
Node<DataType>*originalptr=top->next;
while (originalptr != NULL)
{
originalptr=originalptr->next;
copyptr->next=new Node<DataType>;
copyptr=copyptr->next;
copyptr->info=originalptr->info;
}
}
template <class DataType> // Copy Constructor
Stack<DataType>::Stack(const Stack<DataType> &element)
{
deepCopy(element);
}
template <class DataType> // Destructor
Stack<DataType>::~Stack()
{
makeEmpty();
}
template <class DataType> // Overload assignment operator
Stack<DataType> & Stack<DataType>::operator =(const Stack<DataType> &element)
{
if (this == &element)
return *this ;
makeEmpty();
deepCopy(element);
return *this ;
}
And this is the main file provided by my professor
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
#include <iostream >
#include "stack.h"
using namespace std ;
int main ()
{
Stack < int > s1 , s2 ;
int element ;
s1 . push (1); s1 . push (2); s1 . push (3);
s1 . pop ( element );
cout << " s1 popped element : " << element << endl ;
s2 = s1 ;
s2 . push (4);
s2 . pop ( element );
cout << " s2 popped element : " << element << endl ;
s1 . pop ( element );
cout << " s1 popped element : " << element << endl ;
s2 . makeEmpty ();
s2 . isEmpty () ? cout << " s2 is empty \n" : cout << " s2 is not empty \n " ;
system ("pause" );
return 0;
}
Oct 22, 2014 at 8:19pm UTC
http://www.cplusplus.com/forum/general/112111/
In instantiation of ‘void Stack<DataType>::deepCopy(const Stack<DataType>&) [with DataType = int]’:
73:63: warning: unused parameter ‘original’ [-Wunused-parameter]
your copy does not use the value to copy from
that can't be right
Also
1 2
#include <iostream >
// ^that space shouldn't be there
Topic archived. No new replies allowed.