Lines 13, 19 etc.: Why do you have +1 and +2? I think you will find it easier if
current
is the current number of items in the vector and
max
is the number than can go in without resizing it (aka the capacity).
Lines 25 & 26, you don't need to allocate new items on the heap. In fact you'll just leak them anyway. These lines should be
currentlyList[i] = a[i];
Lines 34-37: Why does add() put the item on the front? If you look at std::vector, you'll notice that you can only push items on the back. This is because adding items to the front is very expensive since you have to move all the existing items.
Line 38: What if i is negative? I should probably be a size_t instead since size_t is unsigned.
Line 40: Okay, it looks like
current
is the max current index
Line 55: Pass a by reference to avoid copying it: push_front(TemplateClass &a)
Line 57: current+2? so max is...? Hmm...
Line 43: add an else here and print out a message if the index is out of bounds. You can remove the message after you have the code working. I believe std::vector throws an exception.
Line 73: You increment the size here but I think you mean to check the old size at line 76. So you're always executing the delete at line 78. Actually this is a good thing because the it doesn't matter what the current size is, you want to delete currentlyList if it isn't NULL. This is an example of code that works by accident, or what I like to call "cooperating bugs." Two bugs offset each other to create working code. If you fix one bug, you will expose the other one.
I think what you want to do here is move line 73 to after line 82: adjust current and currentlyList at the same time. Then delete if currentlyList is non-null:
1 2 3 4 5
|
if (currentlyList) {
delete[] currentlyList;
}
currentlyList = temp;
current++;
|
I don't see a double delete here. Maybe the code that calls it is doing something funny. There are other things that should be changed, but they are slightly more advanced. Let's get the code working first and then clean it up.
Line 76: