Vector resize function not working properly c++

368 Views Asked by At

I have been making a custom vector class, however I have been bumping into a problem. The problem being that my vector just won't resize, the size stays 0. Anyone know what the problem is?

Thanks in advance!

void resize(const int newSize) {
            // If size is zero, give error
            if (newSize < 0) {
                throw std::out_of_range("Vector");
            }
            // If the given size is smaller than the size that it is now, just ignore
            else if (newSize < capacity) {
                return;
            } 
            // Else, make a new array and copy everything over
            T* newArr = new T[newSize];
            for (int i = 0; i < capacity; i++) {
                newArr[i] = data[i];
            }
            delete[] data;
            capacity = newSize;
            data = newArr;
        }

EDIT 1: Here is the push_back function where resize is needed:

template <typename T> void pushBack(Vector<T>& vector, T& value){
    unsigned int oldSize = vector.size();
    vector.resize(oldSize+1);
    vector.at(oldSize) = value;
}

EDIT 2: Here are the vector deffinitions:

        Vector(const int newSize) {
            if (newSize < 0) {
                throw std::out_of_range("Vector");
            }
            data = new T[newSize];
            capacity = newSize;
            actualSize = 0;
        }
0

There are 0 best solutions below