title: overload operator [ ] to enable element assignment
body: <p>I've already overloaded operator [ ] to enable element access.</p>
  
  <p><strong>definition</strong></p>
  
  <pre><code>double Matrix::operator[ ](const &amp; int i){
       return data[i]; // data[] is a member array: thrust:: device_vector
  }
  </code></pre>
  
  <p><strong>usage</strong></p>
  
  <pre><code>Matrix m = ... // initialize
  std::cout &lt;&lt; m[3] &lt;&lt; std::endl;
  </code></pre>
  
  <p>But now I want to access element by index so that assign new value to it. </p>
  
  <p><strong>usage</strong></p>
  
  <pre><code>m[3] = 0.;   
  </code></pre>
  
  <p>I understand the return value of the operator overload function must be an lvalue. I guess I should return a reference, but not sure how this is done. Thanks!</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Now I changed my definition to double &amp;, but it still complains:</p>
  
  <pre><code> error: initial value of reference to non-const must be an lvalue
  </code></pre>
  
  <p>The array refers to a thrust::device_vector, which can be assigned value by index:</p>
  
  <pre><code>Thrust::device_vector&lt;double&gt; M(10);
  M[3] = 0.;
  </code></pre>