0

I created a class that created an array of ints and stored them as a vector would. I had it all working, but then I had to turn it into a template so that I could store more than just ints. I am getting the following errors and am not sure how to address them:

  • subscript is not of integral type (myvector.h line 70)
    • which is the line that says (vectorArray[vectorSize] = n;)
  • 'initializing': conversion from 'double' to 'unsigned int', possible loss of data (myvector.h line 88)
    • Which is the line that says (T *tempArray = new T[newCapacity];)
  • subscript is not of integral type porj08 (myvector.h line 98)
    • Which is the line that says (tempArray[i] = vectorArray[i];)
  • 'initializing': conversion from 'double' to 'unsigned int', possible loss of data (myvector.h line 102)
    • Which is the line that says (vectorArray = new T[newCapacity];)
  • subscript is not of integral type porj08 (myvector.h line 113)
    • Which is the line that says (vectorArray[vectorSize] = n;)

It obviously has something to do with vectorArray but I can not figure out for the life of me what I did wrong.

MyVector.h

#pragma once
#include <iostream>
#include "stdafx.h"

using namespace std;

template <class T>
class MyVector
{

private:
    T vectorSize;
    T vectorCapacity;
    T *vectorArray;

public:
    MyVector() {
        vectorArray = new T[10];
    }
    T size();
    T capacity();
    void clear();
    void push_back(T n);
    T at(T n);

    friend ostream& operator<<(ostream& os, MyVector vt);

    MyVector operator=(MyVector&);
};

/*
 * TEMPLATE FUNCTIONS
 */

//Return array size
template<class T>
T MyVector<T>::size()
{
    return vectorSize;
}

// Return array capacity
template<class T>
T MyVector<T>::capacity()
{
    return vectorCapacity;
}

// clear array values
template<class T>
void MyVector<T>::clear()
{
    for (int i = 0; i < sizeof(vectorArray); i++)
    {
        vectorArray[i] = '\0';
    }

    vectorSize = 0;
    vectorCapacity = 2;
}

// Add number to array and double array size if needed
template<class T>
void MyVector<T>::push_back(T n)
{
    int test = 100;
    if (vectorCapacity > vectorSize)
    {
        vectorArray[vectorSize] = n;
        vectorSize++;

    }
    else {

        if (vectorCapacity == 0) {
            vectorArray = new T[4];
            vectorArray[0] = n;
            vectorCapacity = 4;
            vectorSize++;
        }
        else {

            T newCapacity = vectorCapacity * 2;

            // Dynamically allocate a new array of integers what is somewhat larger than the existing array.An algorithm that is often used is to double the size of the array.

            T *tempArray = new T[newCapacity];

            // Change capacity to be the capacity of the new array.

            vectorCapacity = newCapacity;

            // Copy all of the numbers from the first array into the second, in sequence.

            for (T i = 0; i < MyVector::size(); i++)
            {
                tempArray[i] = vectorArray[i];
            }

            delete[] vectorArray;
            vectorArray = new T[newCapacity];

            for (int i = 0; i < MyVector::size(); i++)
            {
                vectorArray[i] = tempArray[i];
            }

            delete[] tempArray;

            // Add the new element at the next open slot in the new array.

            vectorArray[vectorSize] = n;

            // Increment the size;

            vectorSize++;

        }
    }
}


// Return Value and given point in array
template<class T>
T MyVector<T>::at(T n)
{
    return vectorArray[n];
}

Main.cpp

#include "stdafx.h"
#include <string>
#include "MyVector.h"

const double FRACTION = 0.5;


int main()
{
    cout << "\nCreating a vector of doubles named Sam\n";
    MyVector<double> sam;

    cout << "\nPush 12 values into the vector.";
    for (int i = 0; i < 12; i++)
        sam.push_back(i + FRACTION);

    cout << "\nHere is sam: ";
    cout << sam;
    cout << "\n---------------\n";

    cout << "\nCreating an empty vector named joe";
    MyVector<double> joe;

    // test assignment
    joe = sam;

    cout << "\nHere is joe after doing an assignment:\n ";
    cout << joe;
    cout << "\n---------------\n";

    // test the copy constructor
    MyVector<double> bill = sam;

    cout << "\nHere is bill after creating it using the copy constructor:\n ";
    cout << bill;
    cout << "\n---------------\n";

    cout << endl;
    system("PAUSE");
    return 0;
}
LogicStuff
  • 19,397
  • 6
  • 54
  • 74
Brandon Turpy
  • 883
  • 1
  • 10
  • 32
  • 1
    Before you changed to the template version, some uses `int` in your class referred to the contents of your container and those were correctly changed to `T`. But some uses of `int` in the original referred to the structure of the container and should not have been changed to `T`. – JSF Nov 22 '15 at 22:22

1 Answers1

3
T vectorSize;
T vectorCapacity;

If T is float for example, these will be float, obviously. You don't want that. Type of these members should not be dependent on the type T of elements, make it std::size_t, for example:

std::size_t vectorSize;
std::size_t vectorCapacity;

Then, beware sizeof(pointer) (you used one in clear), use vectorSize or vectorCapacity for loop conditions instead. That should cover all.

Community
  • 1
  • 1
LogicStuff
  • 19,397
  • 6
  • 54
  • 74