title: Why does my overloaded casting operator not have access to private members?
body: <p>I'm attempting to implement an overloaded casting operator on my templated <code>array2d</code> class using type <code>T</code>. So I'm to cast from <code>array2d&lt;T&gt;</code> to a new <code>array2d&lt;E&gt;</code>.</p>
  
  <p>I'm able to perform the casting itself but problems arise when I try to set the casted data to the new instance of <code>array2d&lt;E&gt;</code>. The compiler tells me that the casting operator doesn't have access to the private members of <code>array2d</code></p>
  
  <p>Here's where I am so far (edited out unrelated code for brevity)</p>
  
  <p><strong>array2d.h</strong></p>
  
  <pre><code>template&lt;typename T&gt;
  class array2d {
  private:
      // Member Variables
      T** data;
      size_t width, height;
  public:
      // constructors, methods, etc...
  
      // Cast Operator
      template&lt;typename E&gt;
      operator array2d&lt;E&gt;() const;
  };
  
  // Other overloaded operators...
  
  // Overloaded Casting Operator
  template&lt;typename T&gt;
  template&lt;typename E&gt;
  array2d&lt;T&gt;::operator array2d&lt;E&gt;() const{
      // Create new instance
      array2d&lt;E&gt; castedArr(width, height);
      // Allocate memory for the casted data, then cast each element
      E** newData = new E*[castedArr.get_height()];
  
      for (size_t i = 0; i &lt; castedArr.get_height(); i++){
          newData[i] = new E[castedArr.get_width()];
          for (size_t j = 0; j &lt; castedArr.get_width(); j++){
              newData[i][j] = (E)data[i][j];
          }
      }
      // issue here, can't set data because it's private.
      castedArr.data = newData;
  
      delete [] newData;
      newData = nullptr;
  
      return castedArr;
  }
  </code></pre>
  
  <p><strong>main.cpp</strong></p>
  
  <pre><code>#include "array2d.h"
  
  int main(int argc, char *argv[]) {
  // Cast Operator
      // Create an array2d&lt;T&gt; of
      // width = 5
      // height = 5
      // fill all elements with 42.1
      array2d&lt;double&gt; x(5, 5, 42.1);
  
      // Create a new array exactly the same as
      // x, where x is casted to int
      array2d&lt;int&gt; y = (array2d&lt;int&gt;) x;
  
      return 0;
  }
  </code></pre>
  
  <p>This confused me as I have many other overloaded operators that can access the private members just fine using practically the exact same logic.</p>
  
  <p>Why does this happen and what can I do to rectify it?</p>