C++ SimpleVector 使用模板

C++ SimpleVector Using Templates

我正在尝试让我的程序让用户选择他们想要使用的数据类型,1 代表 int,2 代表双精度,3 代表字符串。获取该类型并使其成为我们动态数组的类型。让用户说出他们想要输入多少数据,然后让用户输入数据。

出于某种我不清楚的原因,我的程序在用户输入他们想要使用的类型的任何数字后立即崩溃。

(我还有一些其他方法要实现,但我想先修复这个。所以这就是为什么有未使用的方法。)

这里有什么我没有看到的吗?任何帮助是极大的赞赏。非常感谢。

#include <iostream>
#include <cstdlib>

using namespace std;

template <class T>
class SimpleVector
{
private:
    T *tempPointer;
    int lengthOfArray;

public:
    SimpleVector();
    ~SimpleVector();
    SimpleVector(int lengthOfArray);
    SimpleVector(const SimpleVector& copy);
    int getArraySize();
    T getElementAt(int n);
    T & operator[](int index);
};

// default no-arg constructor
template <class T>
SimpleVector<T>::SimpleVector()
{
    tempPointer = NULL;
    lengthOfArray = 0;
}

// destructor for deallocating memory
template <class T>
SimpleVector<T>::~SimpleVector()
{
    delete [] tempPointer;
}

// single argument constructor
template <class T>
SimpleVector<T>::SimpleVector(int dynamicArray)
{
    lengthOfArray = dynamicArray;
    tempPointer = new T[lengthOfArray];
}

// Copy constructor
template <class T>
SimpleVector<T>::SimpleVector(const SimpleVector& copy)
: lengthOfArray(copy.lengthOfArray), tempPointer(new int[copy.lengthOfArray])
{
    int newSize = copy->size();
    tempPointer = new T[newSize];

    for(int i = 0; i < newSize; i++){
        tempPointer[i] = copy.tempPointer[i];
    }
}

// gets the size of the dynamic array
template <class T>
int SimpleVector<T>::getArraySize()
{
    return lengthOfArray;
}

// returns element from array at specified position
template <class T>
T SimpleVector<T>::getElementAt(int n)
{
    return *(tempPointer + n);
}

// returns reference to the element in array indexed by subscript
template <class T>
T & SimpleVector<T>::operator[](int index)
{
    return this->tempPointer[index];
}

int main()
{
    int dataType;
    int dataSize = 0;
    char keepGoing;

    do{
        cout << "What type of data do you want to enter?\n(1 for integer, 2 for double and 3 for strings)" << endl;
        cin >> dataType;
        cout << "How many data inputs? " << endl;
        cin >> dataSize;

        SimpleVector <int> list1(dataSize); 
        if (dataType == 1) {
            SimpleVector <int> list1(dataSize);
        }
        else if (dataType == 2) {
            SimpleVector <double> list1(dataSize);
        }
        else if (dataType == 3) {
            SimpleVector <string> list1(dataSize);
        }
        else {
            cout << " That's not an available option. Bye! " << endl;
            return 0;
        }          

        cout << "Please enter the data:" << endl;
        for (int i = 0; i <= dataSize; i++) {
            cin >> list1[i];
        }


        cout << "Do you want to enter data again? (y/n?)" << endl;
        cin >> keepGoing;
    }while((keepGoing == 'Y') | (keepGoing == 'y'));
    return 0;
}

For some reason not clear to me, my program crashes right after the user enters any number for the type they want to use.

我建议您在编写程序时多次测试它。正如@Jarod42 在他的评论中所说,您的 if 语句并没有真正起到多大作用,因为您的

SimpleVector <type> list1(dataSize);

在 { } 后被销毁。
所以基本上无论用户输入什么数字,您的 SimpleVector 都将始终是 int 类型。

现在当您尝试:

cout << "Please enter the data:" << endl;
for (int i = 0; i <= dataSize; i++) {
cin >> list1[i];
}

您正在调用函数:

template <class T>
T & SimpleVector<T>::operator[](int index)
{
    return this->tempPointer[index];
}

此时,您的指针指向 NULL,但您正试图访问指向 NULL 的指针的 [index]。这就是你程序崩溃的原因。

编辑:
我不确定这是否是你想要的,但我会试一试:

do{
    cout << "What type of data do you want to enter?\n(1 for integer, 2 for double and 3 for strings)" << endl;
    cin >> dataType;

    cout << "How many data inputs? " << endl;
    cin >> dataSize;

    if (dataType == 1) {
        SimpleVector <int> list1(dataSize);
        cout << "Please enter the data:" << endl;
        for (int i = 0; i <= dataSize; i++) {
            cin >> list1[i];
        }
    }
    else if (dataType == 2) {
        SimpleVector <double> list1(dataSize);
        cout << "Please enter the data:" << endl;
        for (int i = 0; i <= dataSize; i++) {
            cin >> list1[i];
        }
    }
    else if (dataType == 3) {
        SimpleVector <string> list1(dataSize);
        cout << "Please enter the data:" << endl;
        for (int i = 0; i <= dataSize; i++) {
            cin >> list1[i];
        }
    }
    else {
        cout << " That's not an available option. Bye! " << endl;
        return 0;
    }

    cout << "Do you want to enter data again? (y/n?)" << endl;
    cin >> keepGoing;
}while((keepGoing == 'Y') | (keepGoing == 'y'));