将数组类切换到模板时出错

时间:2015-11-22 22:15:12

标签: c++ arrays templates

我创建了一个创建int数组的类,并将它们存储为矢量。我把它全部工作了,但后来我不得不把它变成一个模板,以便我可以存储的不只是int。我收到以下错误,不知道如何解决它们:

  • 下标不是整数类型(myvector.h第70行)
    • 这是说(vectorArray [vectorSize] = n;)
    • 的行
  • '初始化':来自' double' to' unsigned int',可能丢失数据(myvector.h 88行)
    • 哪条线说(T * tempArray = new T [newCapacity];)
  • 下标不是整数类型porj08(myvector.h第98行)
    • 说的是哪一行(tempArray [i] = vectorArray [i];)
  • '初始化':来自' double' to' unsigned int',可能丢失数据(myvector.h 102行)
    • 说的是哪一行(vectorArray = new T [newCapacity];)
  • 下标不是整数类型porj08(myvector.h第113行)
    • 哪一行(vectorArray [vectorSize] = n;)

这显然与vectorArray有关但我无法弄清楚我做错了什么。

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;
}

1 个答案:

答案 0 :(得分:3)

T vectorSize;
T vectorCapacity;

例如,如果Tfloat,则显然会float。你不想要那个。这些成员的类型不应该依赖于元素的T类型,而是将其设为std::size_t,例如:

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

然后,beware sizeof(pointer)(您在clear中使用了一个),使用vectorSizevectorCapacity代替循环条件。这应该涵盖所有。