title: Matrix Operator*= c++ returns error
body: <p>Good morning. I been dealing with this error for 3 days now and I can't figure it out. I was tasked to create a header file for a series of matrix tests to learn the use of of templates on c++. I seem to have all other operators working except for operator*=. I am including the header file, plus the error I am getting.</p>
  
  <pre><code>#ifndef MATRIX_H
  #define MATRIX_H
  #include &lt;vector&gt;
  #include &lt;iostream&gt;
  #include &lt;math.h&gt;
  #include &lt;complex&gt;
  
  using namespace std;
  namespace nkumath {
  
  template &lt;typename T, size_t ROWS, size_t COLS&gt;
  class Matrix {
      friend class Matrix;
  public:
  
      Matrix(const T &amp; init = T()) : elts(ROWS, vector&lt;T&gt;(COLS, init)) {
      };
  
      const vector&lt;T&gt; &amp; operator[](int ROWS) const {
          return elts[ROWS];
      }; //not sure if correct
  
      vector&lt;T&gt; &amp; operator[](int ROWS) {
          return elts[ROWS];
      }; //not sure if correct
  
      //MatrixAdd
      Matrix &amp; matrixAdd(const Matrix &amp; lhs, const Matrix &amp; rhs) {
          for (int r = 0; r &lt; ROWS; r++) {
              for (int c = 0; c &lt; COLS; c++) {
                  this-&gt;elts[r][c] = lhs[r][c] + rhs[r][c];
              }
          }
          return *this;
      };
  
      //MatrixSubtract
  
      Matrix &amp; matrixSubtract(const Matrix &amp; lhs, const Matrix &amp; rhs) {
  
          for (int r = 0; r &lt; ROWS; r++) {
              for (int c = 0; c &lt; COLS; c++) {
                  this-&gt;elts[r][c] = lhs[r][c] - rhs[r][c];
              }
          }
          return *this;
      };
  
      //MatrixMult
  
      template&lt;size_t INNER&gt;
      Matrix &amp; matrixMult(const Matrix&lt;T, ROWS, INNER&gt; &amp; mat1, const Matrix&lt;T, INNER, COLS&gt; &amp; mat2) {
          for (int i = 0; i &lt; ROWS; i++) {
              for (int j = 0; j &lt; COLS; j++) {
                  //elts[i][j] = 0;
                  for (int k = 0; k &lt; INNER; k++) {
                      elts[i][j] += mat1.elts[i][k] * mat2.elts[k][j];
                  }
              }
          }
          return *this;
      }; //not done
  
      //print function
  
      void print(ostream &amp; out) const {
          for (int i = 0; i &lt; ROWS; i++) {
              for (int j = 0; j &lt; COLS; j++) {
                  out &lt;&lt; elts[i][j];
              }
              out &lt;&lt; "\n";
          }
      };
  
  private:
      vector&lt; vector&lt;T&gt; &gt; elts;
  };
  //Note, you have to define each time a template to avoid having the errors
  //Operator&lt;&lt;
  
  template &lt;typename T, size_t ROWS, size_t COLS&gt;
  ostream &amp; operator&lt;&lt;(ostream &amp; out, const Matrix&lt;T, ROWS, COLS&gt; &amp; elts) {
      elts.print(out);
      return out;
  };
  
  //Operator==
  
  template &lt;typename T, size_t ROWS, size_t COLS&gt;
  bool operator==(const Matrix&lt;T, ROWS, COLS&gt; &amp; lhs, const Matrix&lt;T, ROWS, COLS&gt; &amp; rhs) {
      return true;
  };
  //Operator+
  
  template &lt;typename T, size_t ROWS, size_t COLS&gt;
  Matrix&lt;T, ROWS, COLS&gt; operator+(const Matrix&lt;T, ROWS, COLS&gt; &amp; lhs, const Matrix&lt;T, ROWS, COLS&gt; &amp; rhs) {
      Matrix&lt;T, ROWS, COLS&gt; returnVal;
      return returnVal.matrixAdd(lhs, rhs);
  };
  //Operator-
  
  template &lt;typename T, size_t ROWS, size_t COLS&gt;
  Matrix&lt;T, ROWS, COLS&gt; operator-(const Matrix&lt;T, ROWS, COLS&gt; &amp; lhs, const Matrix&lt;T, ROWS, COLS&gt; &amp; rhs) {
      Matrix&lt;T, ROWS, COLS&gt; returnVal;
      return returnVal.matrixSubtract(lhs, rhs);
  };
  //Operator*
  
  template &lt;typename T, size_t ROWS, size_t COLS&gt;
  Matrix&lt;T, ROWS, COLS&gt; operator*(const Matrix&lt;T, ROWS, COLS&gt; &amp; lhs, const Matrix&lt;T, ROWS, COLS&gt; &amp; rhs) {
      Matrix&lt;T, ROWS, COLS&gt; returnVal;
      return returnVal.matrixMult(lhs, rhs);
  };
  
  //Operator+=
  
  template &lt;typename T, size_t ROWS, size_t COLS, typename C&gt;
  Matrix&lt;T, ROWS, COLS&gt; operator+=(Matrix&lt;T, ROWS, COLS&gt; &amp; lhs, const C &amp; rhs) {
      //Matrix&lt;T,ROWS,COLS&gt; returnVal;
      lhs.matrixAdd(lhs, rhs);
      return lhs;
  };
  
  //Operator-=
  
  template &lt;typename T, size_t ROWS, size_t COLS, typename C&gt;
  Matrix&lt;T, ROWS, COLS&gt; operator-=(Matrix&lt;T, ROWS, COLS&gt; &amp; lhs, const C &amp; rhs) {
      lhs.matrixSubtract(lhs, rhs);
      return lhs;
  };
  //Operator*=
  
  template &lt;typename T, size_t ROWS, size_t COLS, typename C&gt;
  Matrix&lt;T, ROWS, COLS&gt; operator*=(Matrix&lt;T, ROWS, COLS&gt; &amp; lhs, const C &amp; rhs) {
      lhs.matrixMult(lhs, rhs);
      return lhs;
  };
  //Operator/=
  
  template &lt;typename T, size_t ROWS, size_t COLS&gt;
  Matrix&lt;T, ROWS, COLS&gt; operator/=(const Matrix&lt;T, ROWS, COLS&gt; &amp; lhs, const int rhs) {
      Matrix&lt;T, ROWS, COLS&gt; returnVal(rhs);
      for (int r = 0; r &lt; ROWS; r++) {
          for (int c = 0; c &lt; COLS; c++) {
              returnVal[r][c] = lhs[r][c] / returnVal[r][c];
          }
      }
      return returnVal;
  };
  //Operator%=
  
  template &lt;typename T, size_t ROWS, size_t COLS&gt;
  Matrix&lt;T, ROWS, COLS&gt; operator%=(const Matrix&lt;T, ROWS, COLS&gt; &amp; lhs, const int rhs) {
      Matrix&lt;T, ROWS, COLS&gt; returnVal(rhs);
      for (int r = 0; r &lt; ROWS; r++) {
          for (int c = 0; c &lt; COLS; c++) {
              returnVal[r][c] = lhs[r][c] % returnVal[r][c];
          }
      }
      return returnVal;
  };
      } // namespace Matrix
  
      #endif // MATRIX_H
  </code></pre>
  
  <p>here is my main.cpp. I am not allowed to change this file....</p>
  
  <pre><code>// main.cpp
  // Test driver for Matrix class template project.
  #include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;string&gt;
  #include &lt;cstdlib&gt;  // for rand()
  #include "Matrix.h"
  
  using namespace std;
  using namespace nkumath;
  
  template &lt;typename T, size_t ROWS, size_t COLS&gt;
  void randomize(Matrix&lt;T, ROWS, COLS&gt; &amp; mat)
  // Put random values in a Matrix.
  // Note:  It must be possible to assign T an int value.
  {
  for (size_t i = 0; i &lt; ROWS; i++)
      for (size_t j = 0; j &lt; COLS; j++)
          mat[i][j] = (rand() % 21) - 10; // Random number in range -10,...,+10
  }
  
  struct Complex
  {
  Complex(double re = 0.0, double im = 0.0) : real(re), imag(im) { }
  Complex &amp; operator+=(const Complex &amp; rhs)
  {
      real += rhs.real;
      imag += rhs.imag;
      return *this;
  }
  Complex &amp; operator-=(const Complex &amp; rhs)
  {
      real -= rhs.real;
      imag -= rhs.imag;
      return *this;
  }
  Complex &amp; operator*=(const Complex &amp; rhs)
  {
      real = real * rhs.real - imag * rhs.imag;
      imag = real * rhs.imag + imag * rhs.real;
      return *this;
  }
  double real;
  double imag;
  };
  Complex operator+(const Complex &amp; lhs, const Complex &amp; rhs)
  {
  return Complex(lhs.real + rhs.real, lhs.imag + rhs.imag);
  }
  Complex operator-(const Complex &amp; lhs, const Complex &amp; rhs)
  {
  return Complex(lhs.real - rhs.real, lhs.imag - rhs.imag);
  }
  Complex operator*(const Complex &amp; lhs, const Complex &amp; rhs)
  {
  return Complex(lhs.real * rhs.real - lhs.imag * rhs.imag, lhs.real * rhs.imag + lhs.imag * rhs.real);
  }
  ostream &amp; operator&lt;&lt;(ostream &amp; out, const Complex &amp; c)
  {
  out &lt;&lt; "(" &lt;&lt; c.real &lt;&lt; " + " &lt;&lt; c.imag &lt;&lt; "i)";
  return out;
  }
  
  
  int main()
  {
  srand(100);
  ofstream out("output.txt");
  
  // Matrix construction, operator[], and printing:
  Matrix&lt;int, 4, 5&gt; m1(2);
  out &lt;&lt; "m1: " &lt;&lt; endl;
  m1.print(out);
  const Matrix&lt;int, 4, 5&gt; m2 = m1;
  out &lt;&lt; "m2: " &lt;&lt; endl &lt;&lt; m2;
  for (size_t i = 0; i &lt; 4; i++)
      m1[i][i] = 5;
  out &lt;&lt; "m1: " &lt;&lt; endl &lt;&lt; m1;
  
  // Tests of const correctness:
  // m2[0][0] = 0; // This line should not compile.
  // m2 += 4; // Neither should this one.
  int n = m2[0][0]; // This line should be okay.
  
  // Scalar operation tests:
  out &lt;&lt; "m1 += 4: "  &lt;&lt; endl &lt;&lt; (m1 += 4);
  out &lt;&lt; "m1 -= 6: " &lt;&lt; endl &lt;&lt; (m1 -= 6);
  out &lt;&lt; "m1 *= 12: " &lt;&lt; endl &lt;&lt; (m1 *= 12);
  out &lt;&lt; "m1 /= 2: " &lt;&lt; endl &lt;&lt; (m1 /= 2);
  out &lt;&lt; "m1 %= 7: " &lt;&lt; endl &lt;&lt; (m1 %= 7);
  
  // Matrix addition and subtraction tests:
  Matrix&lt;int, 4, 5&gt; m3;
  out &lt;&lt; "m3: " &lt;&lt; endl &lt;&lt; m3;
  out &lt;&lt; "m3.matrixAdd(m1, m2): " &lt;&lt; endl &lt;&lt; m3.matrixAdd(m1, m2);
  out &lt;&lt; "m3.matrixSubtract(m1, m2): " &lt;&lt; endl &lt;&lt; m3.matrixSubtract(m1, m2);
  out &lt;&lt; "m2 + m1: " &lt;&lt; endl &lt;&lt; (m2 + m1);
  out &lt;&lt; "m2 - m1: " &lt;&lt; endl &lt;&lt; (m2 - m1);
  
  // Matrix multiplication tests:
  Matrix&lt;int, 2, 3&gt; m4;
  randomize(m4);
  out &lt;&lt; "m4: " &lt;&lt; endl &lt;&lt; m4;
  Matrix&lt;int, 3, 5&gt; m5;
  randomize(m5);
  out &lt;&lt; "m5: " &lt;&lt; endl &lt;&lt; m5;
  Matrix&lt;int, 2, 5&gt; m6;
  out &lt;&lt; "m6.matrixMult(m4, m5): " &lt;&lt; endl &lt;&lt; m6.matrixMult(m4, m5);
  Matrix&lt;int, 2, 5&gt; m7;
  matrixMult(m4, m5, m7);
  out &lt;&lt; "m6 == m7: " &lt;&lt; (m6 == m7) &lt;&lt; endl;
  out &lt;&lt; "m6 == m4 * m5: " &lt;&lt; (m6 == m4 * m5) &lt;&lt; endl;
  
  // Matrices of strings:
  Matrix&lt;string, 3, 4&gt; m8("Hello");
  for (size_t i = 0; i &lt; 3; i++)
      m8[i][i] = "   Hi";
  out &lt;&lt; "m8: " &lt;&lt; endl &lt;&lt; m8 &lt;&lt; endl;
  Matrix&lt;string, 3, 4&gt; m9(" there!");
  out &lt;&lt; "m9: " &lt;&lt; endl &lt;&lt; m9 &lt;&lt; endl;
  out &lt;&lt; "m8 + m9: " &lt;&lt; endl &lt;&lt; m8 + m9 &lt;&lt; endl;
  Matrix&lt;string, 4, 5&gt; m10(", Goodbye!");
  //out &lt;&lt; m8 * m10 &lt;&lt; endl; // This line should not compile.
  
  // Matrices of Complex:
  Matrix&lt;Complex, 2, 8&gt; m11;
  randomize(m11);
  Complex c(1, -3);
  m11 += c;
  out &lt;&lt; "m11: " &lt;&lt; endl &lt;&lt; m11 &lt;&lt; endl;
  Matrix&lt;Complex, 8, 3&gt; m12;
  randomize(m12);
  m12 -= c;
  out &lt;&lt; "m12: " &lt;&lt; endl &lt;&lt; m12 &lt;&lt; endl;
  out &lt;&lt; "m11 * m12: " &lt;&lt; endl &lt;&lt; m11 * m12 &lt;&lt; endl;
  
  out.close();
  }
  </code></pre>
  
  <p>The error I am getting is</p>
  
  <pre><code>Error   1   error C2784: 'nkumath::Matrix&lt;T,ROWS,COLS&gt; &amp;nkumath::Matrix&lt;T,ROWS,COLS&gt;::matrixMult(const nkumath::Matrix&lt;T,4,INNER&gt; &amp;,const nkumath::Matrix&lt;T,INNER,5&gt; &amp;)' : could not deduce template argument for 'const nkumath::Matrix&lt;T,INNER,5&gt; &amp;' from 'const int' e:\documents and settings\pato\my documents\visual studio 2010\projects\thematrix\thematrix\matrix.h    136
  </code></pre>
  
  <p>I am using visual studio c++ 2010. I tried to compile the header on linux gcc4 and I goth a whole different set of errors, so I am going to finish this project in windows. Either way just need someone to point me to the right direction, I have been looking at this for too long.
  Thank you!
  PJ</p>