Martices multiplication using c++ template
<p>Since you were so helpful, I have another question.</p>

<p>I have implemented matrices multiplication using templates, but I can not compile my code.</p>

<p>Here it is.</p>

<p>matrix.h:</p>

<pre><code>#ifndef __MATRIX_H__
#define __MATRIX_H__

template &lt;class T, int rows, int cols&gt; class matrix {
public:
    T mat[rows][cols];
    matrix();
    matrix(T _mat[rows][cols]);
    matrix operator+(const matrix&amp; b);
};

template &lt;class T, int rows, int cols&gt; matrix &lt;T,rows,cols&gt; :: matrix (T _mat[rows][cols]){
    for (int i=0; i&lt;rows; i++){
        for (int j=0; j&lt;cols; j++){
            mat[i][j] = _mat[i][j];
        }
    }
}

template &lt;class T, int rows, int cols&gt; matrix &lt;T,rows,cols&gt; :: matrix (){
    for (int i=0; i&lt;rows; i++){
        for (int j=0; j&lt;cols; j++){
            mat[i][j] = 0;
        }
    }
}

template &lt;class T, int rows, int cols&gt; matrix &lt;T,rows,cols&gt; matrix &lt;T,rows,cols&gt;::operator+(const matrix&lt;T, rows, cols&gt;&amp; b){
    matrix&lt;T, rows, cols&gt; tmp;
    for (int i=0; i&lt;rows; i++){
        for (int j=0; j&lt;cols; j++){
            tmp.mat[i][j] = this-&gt;mat[i][j] + b.mat[i][j];
        }
    }
    return tmp;
}

template &lt;class T, int rows, int cols&gt; template &lt;int new_cols&gt; matrix &lt;T,rows,new_cols&gt; matrix &lt;T,rows,cols&gt;::operator*(const matrix&lt;T, cols, new_cols&gt;&amp; b){
    matrix&lt;T,rows,new_cols&gt; tmp;
    int i, j, k;
    T sum;
    for(i=0; i&lt;rows; i++){
        for(j=0; j&lt;new_cols; j++){
           sum = 0;
           for (k=0; k&lt;cols; k++){
               sum += this-&gt;mat[i][k] * b.mat[k][j];
           }
           tmp.mat[i][j] = sum;
        }
    }
    return tmp;
}



#endif
</code></pre>

<p>matrix.cpp:</p>

<pre><code>#include "tar5_matrix.h"
int main(){

    int mat1[2][2] = {1,2,
                      3,4};
    int mat2[2][2] = {5,6,
                      7,8};
    int res[2][2];
    matrix&lt;int, 2, 2&gt; C;
    matrix&lt;int, 2, 2&gt; D;
    matrix&lt;int, 2, 2&gt; A = mat1;
    matrix&lt;int, 2, 2&gt; B = mat2;
    C = A+B;
    D = A*B;

    return 0;

}
</code></pre>

<p>When trying to compile, I get the following errors,</p>

<pre><code>1&gt;  tar5_matrix.cpp
1&gt;c:\users\karin\desktop\lior\study\cpp\cpp_project\cpp_project\tar5_matrix.h(68): error C2039: '*' : is not a member of 'matrix&lt;T,rows,cols&gt;'

1&gt;c:\users\karin\desktop\lior\study\cpp\cpp_project\cpp_project\tar5_matrix.cpp(14): error C2676: binary '*' : 'matrix&lt;T,rows,cols&gt;' does not define this operator or a conversion to a type acceptable to the predefined operator
</code></pre>

<p>Please advise.</p>
