So, i wrote stack, which is using vector strategy in case of being too small. The code compiles well and works until the resize function is called to resize the size of array. Tnx for any help solving this error.
#ifndef STACK_H
#define STACK_H
const int DEFAULT_SIZE = 10;
template <typename T>
class stack
{
private:
T *array;
int top_, size;
public:
stack(): top_(-1), size(DEFAULT_SIZE) { array = new T[DEFAULT_SIZE]; };
~stack(){ delete [] array; array = NULL; };
void push(const T &a)
{
if ( top_ == size )
resize();
array[++top_] = a;
}
void resize()
{
int newSize = size + (size / 2);
T *newArray = new T[newSize];
const T *fromArray = array;
T *toArray = newArray;
for(int i = 0; i < size; i++)
*(toArray++) = *(fromArray++);
delete fromArray;
delete toArray;
delete [] array;
array = newArray;
size = newSize;
}
void pop(){ top_--; }
T top() const
{
if ( top_ >= 0 )
return array[top_];
}
bool empty()
{
if ( top_ < 0 )
return true;
else
return false;
}
};
#endif