title: Template T Class Array
body: <p>I have a (what should be simple) assignment in my C++ class.</p>
  
  <p>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.</p>
  
  <p>Here is what I have came up with but coming up with an error of "Allocation of incomplete type 'T'":</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;new&gt;
  #include &lt;vector&gt;
  using namespace std;
  template &lt;class T&gt;
  class DynArray {
  protected:
  
  int size;
  T ** DynamicArray = new T[size];
  
  public:
  DynArray(){
  void CreateArray(){
  cout &lt;&lt; "Enter size of Array: ";
  cin &gt;&gt; size;
  for (int i = 0; i &lt; size; ++i){
  DynamicArray[i] = new T();
  }
  for (int i = 0; i&lt;size; i++) {
  cout &lt;&lt; "Element " &lt;&lt; i &lt;&lt; ": ";
  cin &gt;&gt; DynamicArray[i];}
  }
  //Sort Array
  void Sort(T a[], int size)
  {
  int idx, pass;
  for (pass=0; pass&lt;size; ++pass){
  for (idx=0; idx&lt;size-1; ++idx){
  if (a[idx] &gt; a[idx+1])
  swap(a[idx], a[idx+1]);}
  }
  for (int i=0; i&lt;size; ++i) {
  for (idx = 0; idx&lt;size-1; ++idx) {
  cout &lt;&lt; a[idx] &lt;&lt; " ";
  }
  }
  }
  void DeleteArray(){
  for (int i = 0; i &lt; size; ++i){
  delete DynamicArray[i];
  }
  delete[] DynamicArray;
  }
  };
  int main() {
  DynArray&lt;class T&gt;();
  return 0;
  }
  </code></pre>
  
  <p>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.  </p>
  
  <p><strong>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.</strong></p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;new&gt;
  #include &lt;algorithm&gt;
  using namespace std;
  
  //Template Class T
  template &lt;class T&gt;
  class DynArray {
  protected:
      int size;
      T * DynamicArray;
  public:
      DynArray(){};
      DynArray(size_t s): size(s) {
          DynamicArray = new T[size];
          for (int i = 0; i&lt;size; i++) {
              cout &lt;&lt; "Element " &lt;&lt; i &lt;&lt; ": ";
              cin &gt;&gt; DynamicArray[i];
  
          }
      }
      //Sort Array
      void Sort(){
          sort(DynamicArray, DynamicArray+size);
          for (int i=0; i&lt;size; i++) {
              cout &lt;&lt; DynamicArray[i] &lt;&lt; endl;
          }
      }
  
      //Clear Heap
      ~DynArray() {
          delete []DynamicArray;
      }
  };
  
  int main() {
      int sizeOfArry;
      cout &lt;&lt; "Enter size of Array: ";
      cin &gt;&gt; sizeOfArry;
  
      //Use as an int Array;
      DynArray&lt;int&gt; intArray = DynArray&lt;int&gt;(sizeOfArry);
      intArray.Sort();
  }
  </code></pre>