title: c++ template: 'Instantiation of variable required here, but no definition is available'
body: <p>I created a matrix template class for a matrix of type <code>T</code>. I get a linker error and a warning which says: <code>Instantiation of variable 'ndmatrix&lt;unsigned int&gt;::Subscriptable::_data' required here, but no definition is available</code>. </p>
  
  <p>The template is defined in a separate header:</p>
  
  <pre><code>ndmatrix.h:
  </code></pre>
  
  <pre class="lang-cpp prettyprint-override"><code>#ifndef ndmatrix_h
  #define ndmatrix_h
  #include &lt;vector&gt;
  #include &lt;functional&gt;
  #include &lt;algorithm&gt;
  
  //Generic matrix class to hold the table levels of type T
  template&lt;class T&gt;
  class ndmatrix{
  
  public:
  
      size_t R; //rows
      size_t C; //cols
  
      //Hack to use [][] operator on std::vector
      class Subscriptable{
      public:
  
          //Default constructor
          Subscriptable(){};
  
          Subscriptable(int idx, size_t R, size_t C) : _idx(idx), _R(R), _C(C) {
              for (int i = 0; i&lt;_C*_R; i++) {
                  _data.push_back(0);
              }
          };
  
          Subscriptable setIdx(int i){
              this-&gt;_idx = i;
              return *this;
          }
  
          //This is [] operator for ndmatrix::Subscriptable
          T operator[](int index) {
              return _data[_C *_idx + index];
          }
  
          auto addElement(T e, int i, int j){
              return _data.insert( _data.begin() + _C*i + j, e);
          };
  
          auto data() { return _data; };
      private:
          size_t _R; //rows
          size_t _C; //cols
          int _idx;
          static std::vector&lt;T&gt; _data; &lt;== Warning: Instantiation of variable 'ndmatrix&lt;unsigned int&gt;::Subscriptable::_data' required here, but no definition is available.
      };
  
      Subscriptable _s;
  
      //This is [] operator for ndmatrix
      Subscriptable operator[](int idx){
          return _s.setIdx(idx);
      };
  
      //Constructors
      ndmatrix();
      ndmatrix(int __C, int __R);
  
      //Member functions
      void addRow(std::vector&lt;T&gt; row);
      void print_to_console( std::function&lt;void(T)&gt; printer ); //printer is used to print one element of type T to the console.
  };
  
  template &lt;class T&gt;
  ndmatrix&lt;T&gt;::ndmatrix(){
      _s = Subscriptable();
  };
  
  template &lt;class T&gt;
  ndmatrix&lt;T&gt;::ndmatrix(int __C, int __R){
      _s = Subscriptable();
      for (int i=0; i&lt;__C*__R; i++) {
          _s.push_back(0);
      }
  };
  
  template &lt;class T&gt;
  void ndmatrix&lt;T&gt;::addRow(std::vector&lt;T&gt; row){
      this-&gt;_s.data().insert( _s.data().end() , row.begin(), row.end() );
  };
  
  template &lt;class T&gt;
  void ndmatrix&lt;T&gt;::print_to_console( std::function&lt;void(T)&gt;printer ) {
      for(int i=0; i&lt;R; i++){
          std::cout &lt;&lt; std::endl;
          for (int j=0; j&lt;C; j++) {
              printer( (*this)[i][j] );
          }
      }
  }
  
  #endif /* ndmatrix_h */
  </code></pre>
  
  <p>Then I try to use this in <code>main.cpp:</code>
  </p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;sstream&gt;
  #include "ndmatrix.h"
  
  typedef unsigned int level_t;
  
  
  //Read table of levels from file into a matrix
  void read_matrix(ndmatrix&lt;level_t&gt;* const mat, const char* filename){
      std::ifstream fileInput(filename);
  
      int rows = 0;
      while (fileInput) {
          std::string line;
          std::getline(fileInput, line, '\n');
  
          std::stringstream strstream(line);
          std::vector&lt;level_t&gt; row;
          level_t level;
          int cols=0;
          while ( strstream &gt;&gt; level ) {
              row.push_back(level);
              cols++;
          }
  
          mat-&gt;addRow(row);
          rows++;
  
          mat-&gt;C = cols;
          mat-&gt;R = rows;
  
          //debug
          std::cout &lt;&lt; line &lt;&lt; std::endl;
      }
  };
  
  int main(int argc, const char * argv[]) {
  
      //tests
      ndmatrix&lt;level_t&gt; matrix;
      read_matrix(&amp;matrix, "table.dat");
  
      // insert code here...
      std::cout &lt;&lt; "Hello, World!\n";
      return 0;
  }
  </code></pre>
  
  <p>When trying to build I get the warning above, and a linker error:</p>
  
  <pre><code>clang: error: linker command failed with exit code 1 (use -v to see invocation)
  </code></pre>