title: Matrix Multiplication with operator overloading
body: <p>I am attempting to create an overloaded operator for a matrix class that I have built. My matrix class stores the matrix in a dynamically allocated multidimensional array. I am simply trying to test my overloaded operator by multiplying two matrices together that are exactly the same and display the output. I am getting weird results and I believe it has to do with one of the conditionals on my for loops. I have however walked through all of my for loops and can find nothing wrong. The matrices I am multiplying together are both 6x6.</p>
  
  <p><strong>My overloaded operator</strong></p>
  
  <pre><code>template &lt;typename T&gt;
  const matrix&lt;T&gt; matrix&lt;T&gt;::operator * (const matrix&amp; right) const
  {
      matrix&lt;T&gt; c = right;
      int sum_elems;
      for( int i = 0; i &lt; this-&gt;rows - 1; ++i)
      {
          for(int j = 0; j &lt; right.cols - 1; ++j)
          {
              sum_elems = 0;
              for( int k = 0; k &lt; right.rows - 1; ++k)
              {
                  sum_elems += this-&gt;the_matrix[i][k] * right.the_matrix[k][j];
              }
  
              c.the_matrix[i][j] = sum_elems;
          }
      }
      return c;
  }                 
  </code></pre>
  
  <p><strong>Now my call to the overloaded operator in my main function:</strong></p>
  
  <pre><code>std::cout &lt;&lt; my_matrix;
  matrix&lt;int&gt; copy_matrix;
  copy_matrix = my_matrix * my_matrix; 
  std::cout &lt;&lt; copy_matrix;
  </code></pre>
  
  <p><strong>My output:</strong></p>
  
  <pre><code>  The Matrix:
   0  1  0  1  1  0
   1  0  1  0  1  1
   0  1  0  1  0  1
   1  0  1  0  1  0
   1  1  0  1  0  1
   0  1  1  0  1  0
     The Matrix:
   -1  33  139587680  18  38  75
   139587680  18  38  75  157  1
   139587712  38  1470  4365  10411  1
   139587744  75  4365  19058932  64514866  0
   139587776  157  10411  64514866  1136204102  1
   139596144  1  1  0  1  0
  </code></pre>
  
  <p>As you can see it seems that I am stepping out of bounds on one of my arrays. I can not seem to find where though. I appreciate your help in advance.                     </p>
  
  <p><strong>Edit: As requested my full implementation of my matrix class</strong></p>
  
  <p><strong>Matrix Definitions:</strong></p>
  
  <pre><code>template &lt;typename T&gt;
  class matrix
  {
      public:
  
          //Default Constructor
          matrix();
  
          //Overloaded Constructor
          matrix(std::ifstream&amp;, const char*);
  
          //Copy Constructor
          matrix(const matrix&amp;);
  
          //Destructor
          ~matrix();
  
  
          //overloaded operators
          T* operator [] (T);
          const matrix operator * (const matrix&amp;) const;
          matrix&amp; operator = (const matrix&amp;);
          friend std::ostream&amp; operator &lt;&lt; &lt;T&gt; (std::ostream&amp;, const matrix&lt;T&gt;&amp;); 
  
  
      private:
          T** the_matrix;
          unsigned rows, cols;
  </code></pre>
  
  <p><strong>Matrix Implementation:</strong></p>
  
  <pre><code>/* Template version of matrix class */
  /*---------------------------------------------------------------------------*/
  // Default contructor
  template &lt;typename T&gt;
  matrix&lt;T&gt;::matrix() { }
  
  // Overloaded contructor
  template &lt;typename T&gt;
  matrix&lt;T&gt;::matrix( std::ifstream&amp; in, const char* file)
  {
  
      // declare the variables to be used
      T vertices, edges, u, v;
      std::string line;
  
      // open file for reading
      in.open(file);
  
      // get number of vertices
      in &gt;&gt; vertices;
  
  
      // throw away second line   
      std::getline(in, line);
      std::getline(in, line);
  
      // get number of edges and dump them in two arrays
      in &gt;&gt; edges;
      T edge1 [edges];
      T edge2 [edges];
      int j = 0, k = 0;
      for(int a = 0; a &lt; edges; ++a)
      {    
          in &gt;&gt; u &gt;&gt; v;
          edge1[j] = u;
          edge2[k] = v;
          ++j;
          ++k;
      }
  
      in.close();
  
      // Create multi-dim-dynamic array
      rows = vertices;
      cols = vertices;
  
      the_matrix = new T*[rows];
  
      for( int b = 0; b &lt; rows; ++b)
      {
          the_matrix[b] = new T [rows];
      }
  
      // Initialize array values to zero
      for ( int c = 0; c &lt; rows; ++c)
      {
         for( int d = 0; d &lt; cols; ++d)
         {
             the_matrix[c][d] = 0;
         }
      }
  
      // push the edges to the matrix
      for( int e = 0; e &lt; edges; ++e)
      {
          the_matrix[edge1[e] - 1][edge2[e] - 1] = 1;
      }
      for ( int f = 0; f &lt; edges; ++f)
      {
          the_matrix[edge2[f] - 1][edge1[f]-1] = 1;
      }
  
  
  }
  
  // Copy Constructor
  template &lt;typename T&gt;
  matrix&lt;T&gt;::matrix(const matrix&amp; left)
  {
      the_matrix = left.the_matrix;
      rows = left.rows;
      cols = left.cols;
      spath = left.spath;
  }
  
  // Destructor
  template &lt;typename T&gt;
  matrix&lt;T&gt;::~matrix()
  { 
      // Deletes the data in reverse order of allocation
      for( int a = cols; a &gt; 0; --a)
      {
          delete[ ] the_matrix[a];
      }
  
      delete[ ] the_matrix;
  }
  
  // Overloaded * Operator
  template &lt;typename T&gt;
  const matrix&lt;T&gt; matrix&lt;T&gt;::operator * (const matrix&amp; right) const
  {
      matrix&lt;T&gt; c = right;
      T sum_elems;
      for( int i = 0; i &lt; this-&gt;rows - 1; ++i)
      {
          for(int j = 0; j &lt; right.cols - 1; ++j)
          {
              sum_elems = 0;
              for( int k = 0; k &lt; right.rows - 1; ++k)
              {
                  sum_elems += this-&gt;the_matrix[i][k] * right.the_matrix[k][j];
              }
  
              c.the_matrix[i][j] = sum_elems;
          }
      }
      return c;
  }
  
  // Overloaded assignment Operator
  template &lt;typename T&gt;
  matrix&lt;T&gt;&amp; matrix&lt;T&gt;::operator = (const matrix&amp; right)
  {
      this-&gt;the_matrix= right.the_matrix;
      this-&gt;rows = right.rows;
      this-&gt;cols = right.cols;
      this-&gt;spath = right.spath;
      return *this;
  }
  
  // Overloaded &lt;&lt; operator
  template &lt;typename T&gt;
  std::ostream&amp; operator &lt;&lt; (std::ostream&amp; output, const matrix&lt;T&gt;&amp; left)
  {
      // Test screen output to see if correct   
      std::cout &lt;&lt; std::setw(14) &lt;&lt; "The Matrix:" &lt;&lt; '\n';
      for( int a = 0; a &lt; left.rows; ++a)
      {
          for( int b = 0; b &lt; left.cols; ++b)
          {
              std::cout &lt;&lt; ' ' &lt;&lt; left.the_matrix[a][b] &lt;&lt; ' ';
          }
          std::cout &lt;&lt; '\n';
      }
      return output;
  }
  </code></pre>