Template T Class Array

B. N. picture B. N. · Nov 18, 2016 · Viewed 24.6k times · Source

I have a (what should be simple) assignment in my C++ class.

Here is the assignment: Create a class template that contains two private data members: T * array and int size. The class uses a constructor to allocate the array based on the size entered. There is member function that allows the user to fill in the array based on the size. In addition, there is a member function that sorts the array and displays the sorted elements. Use the destructor to delete the array. Develop the main( ) to create two objects to call member functions. As a result, the first object will hold its array of double type, and the other will hold its array of the int type.

Here is what I have came up with but coming up with an error of "Allocation of incomplete type '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;
}

Not sure if I am a complete retard in my line of thinking or if I am just missing a small element. Any help is great.

Thank you @jblixr and @user3655463 for your help. I got it worked out I think after your hints and help. Here is what I came up with for reference if anyone else is working on this.

#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();
}

Answer

jblixr picture jblixr · Nov 18, 2016

I am wondering that whether you are creating a one dimensional array or 2D array, your initial requirements states one dimensional array but you are using 2D array in your code. Anyway, I play along with one dimensional array.

error: allocation of incomplete type 'T'

T *DynamicArray = new T[size];

What you are trying to do here is in-class initialization which can be done for static data members and it is an c++11 extension for non-static data member. So I suggest you not to do so since you are learning. You can only declare the members and don't initialize here.

Even if you make static it cannot be allocated because the template type T is known only after object creation, So the compiler isn't aware of the type it is going to allocate during the compile time. So it should be simply as

T *DynamicArray;

Nested functions

C++ doesn't support nested functions, learn C++ syntax.

Usage of constructors and destructors

A constructor will do the functionality of CreateArray() and the destructor will do the functionlaity of DeleteArray()

Instantiating a template class

You should explicitly mention the type within the angle brackets that the template class is going to use

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

You can also use your own custom class for type T, hope you will learn soon in your class.

DynArray<MyCustomClass> customArray;

If you correct all these things then your final skeleton will be like the below one

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

Simple, isn't it? :)