模板T类数组

Template T Class Array

本文关键字:数组 模板      更新时间:2023-10-16

在我的c++类中有一个(应该是简单的)赋值。

赋值如下:创建一个包含两个私有数据成员的类模板:T * array和int size。该类使用构造函数根据输入的大小分配数组。有一个成员函数允许用户根据大小来填充数组。此外,还有一个成员函数对数组进行排序并显示已排序的元素。使用析构函数删除数组。开发main()来创建两个对象来调用成员函数。因此,第一个对象将保存其double类型的数组,而另一个对象将保存其int类型的数组。

这是我所提出的,但提出了一个错误的"分配不完整类型'T'":

#include <iostream>
#include <new>
#include <vector>
using namespace std;
template <class T>
class DynArray {
protected:
int size;
T ** DynamicArray = new T[size];
public:
DynArray(){
void CreateArray(){
cout << "Enter size of Array: ";
cin >> size;
for (int i = 0; i < size; ++i){
DynamicArray[i] = new T();
}
for (int i = 0; i<size; i++) {
cout << "Element " << i << ": ";
cin >> DynamicArray[i];}
}
//Sort Array
void Sort(T a[], int size)
{
int idx, pass;
for (pass=0; pass<size; ++pass){
for (idx=0; idx<size-1; ++idx){
if (a[idx] > a[idx+1])
swap(a[idx], a[idx+1]);}
}
for (int i=0; i<size; ++i) {
for (idx = 0; idx<size-1; ++idx) {
cout << a[idx] << " ";
}
}
}
void DeleteArray(){
for (int i = 0; i < size; ++i){
delete DynamicArray[i];
}
delete[] DynamicArray;
}
};
int main() {
DynArray<class T>();
return 0;
}

不确定我是一个完全的智障在我的思路,或者如果我只是错过了一个小元素。任何帮助都很好。

感谢@jblixr和@user3655463的帮助。在你的提示和帮助下,我终于把它弄明白了。这是我想到的参考,如果有人在做这个

#include <iostream>
#include <new>
#include <algorithm>
using namespace std;
//Template Class T
template <class T>
class DynArray {
protected:
    int size;
    T * DynamicArray;
public:
    DynArray(){};
    DynArray(size_t s): size(s) {
        DynamicArray = new T[size];
        for (int i = 0; i<size; i++) {
            cout << "Element " << i << ": ";
            cin >> DynamicArray[i];
        }
    }
    //Sort Array
    void Sort(){
        sort(DynamicArray, DynamicArray+size);
        for (int i=0; i<size; i++) {
            cout << DynamicArray[i] << endl;
        }
    }
    //Clear Heap
    ~DynArray() {
        delete []DynamicArray;
    }
};
int main() {
    int sizeOfArry;
    cout << "Enter size of Array: ";
    cin >> sizeOfArry;
    //Use as an int Array;
    DynArray<int> intArray = DynArray<int>(sizeOfArry);
    intArray.Sort();
}

从你提供的描述来看,你想做这样的事情:

#include <iostream>
#include <algorithm>
using namespace std;
    
template <class T>
class DynArray {
    
    protected:
        int size;
        T * DynamicArray;
    public:
        DynArray(size_t s): size(s) {
            DynamicArray = new T[s];
        }
        void CreateArray(){
            size_t size;
            cout << "Enter size of Array: ";
            cin >> size;
            if(size > this->size)
              size = this->size;
            for (int i = 0; i<size; i++) {
                cout << "Element " << i << ": ";
                cin >> DynamicArray[i];
            }
        }
        //Sort Array
        void Sort()
        {
           std::sort(DynamicArray, DynamicArray+size);                                                                                                                                            
        }
        DynArray() {
            delete []DynamicArray;
        }
};
int main() {
    DynArray<double> dob(3);
    DynArray<int> di(3);
    dob.CreateArray();
    di.CreateArray();
    dob.Sort(); di.Sort();
}

                                                                                                                                                                   

我想知道您是否正在创建一维数组或二维数组,您的初始要求是一维数组,但您在代码中使用二维数组。总之,我使用一维数组。

error: allocation of incomplete type 'T'

T *DynamicArray = new T[size];

你在这里要做的是类内初始化,可以为静态数据成员完成,它是一个c++11扩展的非静态数据成员。所以我建议你不要这样做,因为你正在学习。这里只能声明成员,不能初始化。

即使你设置为static,它也不能被分配,因为模板类型T只有在对象创建之后才知道,所以编译器在编译期间不知道它要分配的类型。所以应该写成

T *DynamicArray;
嵌套函数

c++不支持嵌套函数,学习c++语法

构造函数和析构函数的使用

构造函数具有CreateArray()的功能,析构函数具有DeleteArray()的功能

实例化模板类

你应该在尖括号内明确指出模板类将要使用的类型

DynArray<int> intArray; // if you want to use int
DynArray<float> floatArray;

你也可以使用自己的自定义类T类型,希望你能在你的课上很快学会。

DynArray<MyCustomClass> customArray;

如果你纠正了所有这些,那么你最终的骨架就会像下面这个

template <class T>
class DynArray {
protected:
    int size;
    T *DynamicArray ;
public:
    DynArray() {
        // initailize DynamicArray here
        // use your CreateArray() code here
    }
    void sort() {
        // your own sort logic
        // No need to pass the size to this function, its a member function 
    }
    ~DynArray() {
        // use your DeleteArray() code here
    }
};
int main() {
    DynArray<int> intArray;
    intArray.sort()
    return 0;
}
很简单,不是吗?:)