Ok so this is a school exercise so I know a vector would be more appropriate but I’m required to use an array. I’m making a stack implementation using an array. I’m keeping track of the size of the array with the var “cap” and I’m tracking the size of the simulated stack with the var “size”. Where it is erring out at the point when I attempt to push() an item beyond the original array bounds. In the push() method I have an if statement checking the if the “size” (size of the stack) is equal to the cap (size of the array) and if it is I should be creating a new array (call newItems), copying ever thing to the new array, delete the old array to free it up, then recreate it with the new size, and copy the new array (newItems) to the freed up old array. That’s the problem. I think it’s an issue of scope or I failing to recreate it. Ultimately when I attempt to add that last item I get the error.
Also is someone can point out the proper way/place to do the deletes of the arrays that I create with the new opperator that would be great. I'm still hazy on that part.
The first problem I see is that you've declared items to be a fixed-sized array. Fixed-size arrays
cannot be resized. You need items to be a pointer to T.
I did not look beyond the class definition as deleting items will definitely corrupt the heap.
Alright my first though was to change the items array declaration to
T* items[DEFAULT_INITIAL_CAP ];
But that then seems to cause an error down in the copy of the items array to the newItems array (the temp array). The error is that the types don't match. So my first question is if I totaly misunderstood what I was told about the array declaration.
T* items[ DEFAULT_INITIAL_CAP ]; still just declares a fixed length array, but this time the
elements are pointers-to-T instead of T. You want T* items;
arrays are best use when u go over size, so it is best making the array larger then exact or close to the size thats needed but ur pollymorphim solution can work out if complicating programming was just testing ur own skill level.