title: Program doesn't compile when I include .h, works when I include .cpp
body: <p>I have these three files, main.cpp, DynamicArray.cpp, DynamicArray.h. In both cpp files I include DynamicArray.h and I get the following errors.</p>
  
  <pre><code>$ g++ main.cpp DynamicArray.cpp 
  /tmp/ccDGRYws.o: In function `testDynamicArray()':
  main.cpp:(.text+0x11): undefined reference to `DynamicArray&lt;int&gt;::DynamicArray()'
  main.cpp:(.text+0x1d): undefined reference to `DynamicArray&lt;int&gt;::DynamicArray()'
  main.cpp:(.text+0x39): undefined reference to `DynamicArray&lt;int&gt;::~DynamicArray()'
  main.cpp:(.text+0x5a): undefined reference to `DynamicArray&lt;int&gt;::add(int)'
  main.cpp:(.text+0x72): undefined reference to `DynamicArray&lt;int&gt;::size()'
  main.cpp:(.text+0xb8): undefined reference to `DynamicArray&lt;int&gt;::~DynamicArray()'
  main.cpp:(.text+0xc9): undefined reference to `DynamicArray&lt;int&gt;::~DynamicArray()'
  collect2: error: ld returned 1 exit status
  </code></pre>
  
  <p>When I change the DynamicArray.h in  main.cpp to DynamicArray.cpp it works fine. Why is it not working with the .h?</p>
  
  <p>main.cpp</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;assert.h&gt;
  
  #include "DynamicArray.h"
  
  void testDynamicArray(){
  
      DynamicArray&lt;int&gt; array1;
      array1 = DynamicArray&lt;int&gt;();
  
      int initialSize = 10;  
  
      for (int i = 0; i &lt; initialSize; i++) {
          array1.add(i);
      }
  
      assert(array1.size() == initialSize);
  
      std::cout &lt;&lt; "All tests for DynamicArray pass." &lt;&lt; std::endl;
  }
  
  
  
  int main(){
      testDynamicArray();
  }
  </code></pre>
  
  <p>DynamicArray.cpp</p>
  
  <pre><code>#include "DynamicArray.h"
  #include &lt;iostream&gt;
  
  template &lt;typename T&gt;
  DynamicArray&lt;T&gt;::DynamicArray(){
      numElements = 0;
      arraySize = 10;
      theArray = new T[arraySize];
  }
  
  template &lt;typename T&gt;
  DynamicArray&lt;T&gt;::~DynamicArray(){
      delete[] theArray;    
  }
  
  template &lt;typename T&gt;
  int DynamicArray&lt;T&gt;::size(){
      return numElements;
  }
  
  template &lt;typename T&gt;
  bool DynamicArray&lt;T&gt;::isEmpty(){
      return (numElements == 0);
  }
  
  template &lt;typename T&gt;
  void DynamicArray&lt;T&gt;::increaseSize(){
          T* tempArray = new T[arraySize * 2];
  
          for(int i = 0; i &lt;= numElements; i++) {
              tempArray[i] = theArray[i];
          }
  
          delete[] theArray;
          theArray = tempArray;
  
          arraySize *= 2;
  }
  
  template &lt;typename T&gt;
  void DynamicArray&lt;T&gt;::insert(T newElement, int index){
      if (numElements == arraySize) {
          increaseSize();
      }
  
      for (int i = numElements; i &gt;= index ; i--) {
          theArray[i] = theArray[i-1];
      }
  
      theArray[index] = newElement;
      numElements++;
  }
  
  template &lt;typename T&gt;
  void DynamicArray&lt;T&gt;::add(T newElement){
      if(numElements == arraySize)
          increaseSize();
  
      theArray[numElements] = newElement;
  
      numElements++;
  }
  
  template &lt;typename T&gt;
  T DynamicArray&lt;T&gt;::get(int index){
      return theArray[index];
  }
  
  template &lt;typename T&gt;
  bool DynamicArray&lt;T&gt;::deleteIndex(int index){
  
      if(index &lt; 0 || index &gt;= numElements)
          return false;
  
      for (int i = index; i &lt; numElements-1; i++) 
      {
          theArray[i] = theArray[i+1];
      }
  
      numElements--;
  
      return true;
  }
  </code></pre>
  
  <p>DynamicArray.h</p>
  
  <pre><code>#ifndef DYNAMICARRAY
  #define DYNAMICARRAY
  
  template &lt;typename T&gt;
  class DynamicArray{
      private:
      T* theArray;
      int numElements;
      int arraySize;
      void increaseSize();
  
      public:
      DynamicArray();
      ~DynamicArray();
      int size();
      bool isEmpty();
      void insert(T newElement, int index);
      void add(T newElement);
      bool deleteIndex(int index);
      T get(int index);
  };
  
  #endif
  </code></pre>