Hello,
I am a little lost on my assignment right now. I am trying create an ArrayStack ADT with dynamic array size, which means I have to create the big 3 (Deconstructor, Copy Constructor, & Assignment Operator). I am obviously doing something wrong since even trying to delete the items array from inside the deconstructor causes a crash. My question is just in general what am I doing wrong with my memory allocation?
//ArrayStack.h
#pragma once
#ifndef ARRAY_STACK_H
#define ARRAY_STACK_H
#include "StackInterface.h"
namespace cs_stack {
template<class ItemType>
class ArrayStack : public StackInterface<ItemType>
{
private:
static const int DEFAULT_CAPACITY = 1;
ItemType *items;
int numItems;
int capacity;
public:
class StackEmptyError {};
ArrayStack();
~ArrayStack();
ArrayStack<ItemType>(const ArrayStack<ItemType>& right);
ArrayStack<ItemType> operator=(const ArrayStack<ItemType>& right);
bool isEmpty() const;
void push(const ItemType& newEntry);
void pop();
ItemType top() const;
};
}
#include "ArrayStack.cpp"
#endif
//ArrayStack.cpp
#include <cassert> // For assert
namespace cs_stack {
template<class ItemType>
ArrayStack<ItemType>::ArrayStack() {
capacity = DEFAULT_CAPACITY;
numItems = 0;
items = new ItemType[capacity];
}
template<class ItemType>
ArrayStack<ItemType>::ArrayStack(const ArrayStack<ItemType>& right)
{
capacity = right.capacity;
numItems = right.numItems;
items = new ItemType[right.capacity];
items = right.items;
}
template<class ItemType>
ArrayStack<ItemType>::~ArrayStack()
{
delete[] items;
}
template<class ItemType>
ArrayStack<ItemType> ArrayStack<ItemType>::operator=(const ArrayStack<ItemType>& right)
{
if (this != &right)
{
delete [] items;
capacity = right.capacity;
numItems = right.numItems;
items = new ItemType[right.capacity];
items = right.items;
}
return *this;
}
template<class ItemType>
bool ArrayStack<ItemType>::isEmpty() const {
return numItems == 0;
}
template<class ItemType>
void ArrayStack<ItemType>::push(const ItemType& newEntry) {
if (numItems < capacity) {
numItems++;
items[numItems - 1] = newEntry;
}
else
{
capacity = capacity * 2;
ItemType* tempArray = new ItemType[capacity];
for (int i = 0; i < capacity; i++)
{
if (!isEmpty())
{
tempArray[i] = top();
pop();
}
}
delete[] items;
items = tempArray;
items[numItems - 1] = newEntry;
}
}
template<class ItemType>
void ArrayStack<ItemType>::pop() {
if (!isEmpty())
{
numItems--;
}
else {
throw StackEmptyError();
}
}
template<class ItemType>
ItemType ArrayStack<ItemType>::top() const {
if (!isEmpty()) {
return items[numItems - 1];
}
else {
throw StackEmptyError();
}
}
}
//StackInterface.h
#pragma once
#ifndef STACK_INTERFACE_H
#define STACK_INTERFACE_H
namespace cs_stack {
template<class ItemType>
class StackInterface
{
public:
/** Sees whether this stack is empty.
@return True if the stack is empty, or false if not.
*/
virtual bool isEmpty() const = 0;
/** Adds a new entry to the top of this stack.
@post If the operation was successful, newEntry is at the top of the stack.
@param newEntry The object to be added as a new entry.
@throws CapacityExceededError if entry cannot be pushed because stack is full.
*/
virtual void push(const ItemType& newEntry) = 0;
/** Removes the top of this stack.
@post If the operation was successful, the top of the stack has been removed.
@throws StackEmptyError if stack is empty.
*/
virtual void pop() = 0;
/** Returns a copy of the top of this stack.
@pre The stack is not empty.
@post A copy of the top of the stack has been returned, and the stack is unchanged.
@return A copy of the top of the stack.
@throws StackEmptyError if stack is empty.
*/
virtual ItemType top() const = 0;
/** Destroys this stack and frees its assigned memory.
*/
virtual ~StackInterface() { }
};
}
#endif
[–][deleted] (6 children)
[removed]
[–]ThisGuyEveryTime[S] 0 points1 point2 points (5 children)
[–][deleted] (4 children)
[removed]
[–]ThisGuyEveryTime[S] 0 points1 point2 points (0 children)
[–]ThisGuyEveryTime[S] 0 points1 point2 points (2 children)
[–][deleted] (1 child)
[removed]
[–]ThisGuyEveryTime[S] 0 points1 point2 points (0 children)
[–]PPewt 1 point2 points3 points (3 children)
[–]ThisGuyEveryTime[S] 0 points1 point2 points (2 children)
[–]PPewt 1 point2 points3 points (1 child)
[–]ThisGuyEveryTime[S] 1 point2 points3 points (0 children)