g++ linker error: undefined reference
<p>I am writing a templatized <code>Matrix</code> class, and I include the declarations in <code>Matrix.h</code>, the implementation in <code>Matrix.cpp</code>. I have a test file <code>testMatrix.cpp</code>.</p>

<p>The beginning of the files look like this:<br></p>

<p>----Matrix.h----</p>

<pre><code>#ifndef _MATRIX
#define _MATRIX
#include &lt;string.h&gt;
// Definition of Matrix class
#endif
</code></pre>

<p>----Matrix.cpp----</p>

<pre><code>#include "Matrix.h"
#include &lt;iostream&gt;
using namespace std;
// ...Implementation
</code></pre>

<p>---testMatrix.cpp---<br></p>

<pre><code>#include "Matrix.h"
#include &lt;iostream&gt;
using namespace std;
int main () {
  cout &lt;&lt; "Test constructors...\n";
  cout &lt;&lt; "Unitialized matrix (4, 4):\n";
  Matrix&lt;int&gt; mi1 (4, 4);
  mi1.print ();
  cout &lt;&lt; "4*4 matrix initialized to -1:\n";
  Matrix&lt;int&gt; mi2 (4, 4, -1);
  mi2.print();                                                                                                                                    
  cout &lt;&lt; "Constructing mi3 as a copy of mi2:\n";
  Matrix&lt;int&gt; mi3 (mi2);
mi3.print();                                                                                                                                    
  cout &lt;&lt; "Assigning mi3 to mi1:\n";
  mi1 = mi3; 
mi1.print();                                                                                                                                    

  return 0;
}
</code></pre>

<p>The command line I used to compile:</p>

<pre><code>g++ -Wall -lrt -g Matrix.cpp testMatrix.cpp -o testMatrix
</code></pre>

<p>The compiler keeps giving me the error:</p>

<pre><code>/tmp/ccofoNiO.o: In function `main':
/afs/ir/users/t/i/tianxind/practice/Essential_C++/testMatrix.cpp:9: undefined reference to `Matrix&lt;int&gt;::print() const'
collect2: ld returned 1 exit status
</code></pre>

<p>Does anyone have an idea what went wrong here? Thanks so much!</p>
