title: How to define a [ ][ ] operator for a matrixclass in Qt?
body: <p>I am working on my own matrix class in Qt. I know there is a QGenericMatrix class template, but I need to set the size at runtime, which is not possible with this one. Apart from that I see this as a nice project to revive my linear algebra knowledge by implementing this.</p>
  
  <p>However, I have already been able to define the * operator (multiplication) as follows:</p>
  
  <p>MyMatrix.h</p>
  
  <pre class="lang-cpp prettyprint-override"><code>public:
     MyMatrix(int rows, int cols, double initValues=0.0);        // constructor to create NxM matrix with N=col, M=rows
  
     MyMatrix&amp; operator*(double value);                           // multiply matrix with a double 
  private:
      int rows;
      int cols;
      double **mat;
      void initMatrix(int rows=1, int cols=1);        // initialise the matrix, if no rows,cols are given it creates a "1x1 matrix"
  </code></pre>
  
  <p>MyMatrix.cpp</p>
  
  <pre class="lang-cpp prettyprint-override"><code>// constructor
  MyMatrix::MyMatrix(int rows, int cols, double initValues)
  {
      initMatrix(rows, cols);
      for (int i = 0; i &lt; this-&gt;rows; ++i) {
          for (int j = 0; j &lt; this-&gt;cols; ++j) {
              this-&gt;mat[i][j] = initValues;
          }
      }
  }
  // multiply each element in matrix by value
  MyMatrix&amp; MyMatrix::operator*(double value) {
      for (int i = 0; i &lt; this-&gt;rows; ++i) {
          for (int j = 0; j &lt; this-&gt;cols; ++j) {
              this-&gt;mat[i][j] = this-&gt;mat[i][j] * value;
          }
      }
      return *this;
  }
  // initialise all matrix cells
  void MyMatrix::initMatrix(int rows, int cols)
  {
      this-&gt;rows = rows;                          // assign argument to member variable rows
      this-&gt;cols = cols;                          // assign argument to member variable cols
  
      this-&gt;mat = new double*[this-&gt;rows];        // initialise mat with list of doubles ptrs with length rows
      for (int i = 0; i &lt; this-&gt;rows; ++i) {      // iterate over each row-element
          this-&gt;mat[i] = new double[this-&gt;cols];  // initialise each rows-element with list of doubles with length cols
      }
  }
  </code></pre>
  
  <p>main.cpp</p>
  
  <pre class="lang-cpp prettyprint-override"><code>    int rows = 2;
      int cols = 3;
      MyMatrix mat1(rows, cols, 1.0);             // creates matrix with all elements 1.0
  
      mat1 = mat1 * 3.0;
  </code></pre>
  
  <p>Note I extracted only the relevant parts, the class has grown already, so I guess posting the all three files completely would be more confusing.</p>
  
  <p>So far so good. The above seems to do what it should.</p>
  
  <hr>
  
  <p>Now, I want to be able to directly access each element in the matrix. Similar to how one can access elements in a QVector, like so:</p>
  
  <p>Read an element:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>   double temp = mat1[2][2]               // read the element in row=2, column=2
  </code></pre>
  
  <p>Write to an element:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>   double temp = 17;
     mat1[2][2] = temp              // set value of element in row=2, column=2 to given double temp (here 17).
  </code></pre>
  
  <p>But I do not know how to define this [][] operator. I tried the following definition analog to the multiplication with a double, and because I need to give the row and column. I thought I try:</p>
  
  <p>MyMatrix.h</p>
  
  <pre class="lang-cpp prettyprint-override"><code>   MyMatrix&amp; operator[int c][int r](double value);    // write
     MyMatrix&amp; operator[int c][int r]();                // read
  </code></pre>
  
  <p>The implementation to overwrite/read the element in row r and column c which I have in mind should look like this:</p>
  
  <p>MyMatrix.cpp</p>
  
  <pre class="lang-cpp prettyprint-override"><code>// write to element
  MyMatrix&amp; MyMatrix::operator[int r][int c](double value) {
      this-&gt;mat[r][c] = value;
      return *this;
  }
  // read to element
  double&amp; MyMatrix::operator[int r][int c]() {
      return this-&gt;mat[r][c];
  }
  </code></pre>
  
  <hr>
  
  <p>But that does not do the Trick.</p>
  
  <p>Btw: Even before compiling QtCreator says:</p>
  
  <pre><code>/path/MyMatrixClass/mymatrix.cpp:60: error: expected ']'
  /path/MyMatrixClass/mymatrix.cpp:60: to match this '['
  /path/MyMatrixClass/mymatrix.cpp:60: error: expected '(' for function-style cast or type construction
  /path/MyMatrixClass/mymatrix.cpp:61: error: use of undeclared identifier 'r'
  /path/MyMatrixClass/mymatrix.cpp:61: error: use of undeclared identifier 'c'
  </code></pre>
  
  <p>I tried already searching a for these errors, but so far I could not find anything giving me a clue to what I want to achieve.</p>
  
  <p>So, perhaps someone can give me a link on where to look for an example or some advice on how I can achieve what I want.</p>
  
  <hr>
  
  <p>PS: Later I also want to be able to extract a certain row, or certain column, but I guess (=hope) that should be straight forward once I know how to handle the [][] operator the right way.</p>
  
  <p>This is the first time I am really defining my own operators for a class. And I think I got the general idea from the * operator. (I have also + and - operators already working). However, until now I used Qt mostly for GUI building, simple data handling with QVectors, plotting spectra and alike. So, I guess I am just missing some basic Qt/c++ syntax.</p>