I know there's a lot of similar questions out there, but I haven't found anything that helps yet. I've been at this for several hours now and it's driving me crazy. I get a segmentation fault when a destructor is called for a variable that was created by the copy constructor.
//Copy Constructor
Stack::Stack(const Stack &aStack)
{
size = 0; //this is incremented as new items are pushed onto the stack.
cap= aStack.cap;
items = new int[aStack.cap]();
for (int i = 0; i < aStack.size; i++)
this->push(aStack.items[i]); //Adds an item if not full and increments size
// I have also tried: items[i] = aStack.items[i]
}
//Destructor
Stack::~Stack()
{
cap= 0;
size= 0;
delete [] items;
items = NULL;
}
I get the feeling I'm doing the copy constructor wrong, but I can't figure out what it is.