bool UniqueVector<T>::push_front(const T& data){
//If data is already in the array, return false
for(int index=0; index < arraySize; index++){
if(array[index] == data)
returnfalse;
}
//made temp, double capacity, copy elements from array to temp
// deallocated array and pointed array to temp
if(arraySize == arrayCapacity){
arrayCapacity*=2;
T* temp = new T[arrayCapacity];
for(int index=0; index<arraySize; index++)
temp[index] = array[index];
delete [] array;
array = temp;
}
arraySize++;
//Shift the elements of the array to the right
for(int index=arraySize-1; index>0; index=index-1)
array[index] = array[index-1];
array[0] = data;
returntrue;
if(arraySize==0){
array[0] = data;
arraySize++;
returntrue;
}
using the my at function to place data into the front.
1 2 3 4 5 6 7 8 9 10 11 12
template<typename T>
bool UniqueVector<T>::at(unsignedint pos, T& data){
if(pos < arraySize){
//if there's nothing in the position
if(array[pos] == NULL){
returnfalse;
}
data = array[pos];
returntrue;
}
returnfalse;
}
My problem when I try to test the code, when push_front is false, at would still add elements into the front of the array. Any help or insight would be appreciated.
I apologize, but I'm having a hard time debugging this code. When ( testVector.push_front(0) == false); the program is not supposed to push 0 to the front of the array. Temp in this case would still be 2, since the test code
When I compile and tried to run the test case, it fails with because it pushed 0 into the front. I occasionally get a malloc error saying that position being freed is not allocated.
The only obvious thing I see wrong is lines 5-7 in your at snippet which should be entirely removed, but I don't see how it would have the effect you describe. There is source code not presented here that is probably responsible for what you're seeing.
Have you implemented a proper copy constructor and copy assignment operator for your UniqueVector type?