title: Should I overload the assignment operator C++?
body: <p>I have the following code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  class Cell{
    private:
      int score;
      char parent;
    public:
      Cell();
      Cell(int scoreIn, char parentIn);
      int getScore();
      char getParent();
  };
  
  Cell::Cell(){
    score = 0;
    parent = '-';
  }
  
  Cell::Cell(int scoreIn, char parentIn){
    score = scoreIn;
    parent = parentIn;
  }
  
  int Cell::getScore(){
    return score;
  }
  
  char Cell::getParent(){
    return parent;
  }
  
  int main(){
    Cell** nwArray = new Cell*[10];
    for(int i = 0; i &lt; 10; i++){
      nwArray[i] = new Cell[10];
    }
    for(int i = 0; i &lt; 10; i++){
      for(int j = 0; j &lt; 10; j++){
        nwArray[i][j] = new Cell(10, 'q');
        std::cout &lt;&lt; nwArray[i][j].getScore() &lt;&lt; "\t";
      }
    }
  }
  </code></pre>
  
  <p>Compilation results in the following:</p>
  
  <pre><code>g++ test.cpp -o test
  test.cpp: In function ‘int main()’:
  test.cpp:39:39: error: no match for ‘operator=’ in ‘*((*(nwArray + ((sizetype)(((unsigned int)i) * 4u)))) + ((sizetype)(((unsigned int)j) * 8u))) = (operator new(8u), (&lt;statement&gt;, ((Cell*)&lt;anonymous&gt;)))’
  test.cpp:39:39: note: candidate is:
  test.cpp:3:7: note: Cell&amp; Cell::operator=(const Cell&amp;)
  test.cpp:3:7: note:   no known conversion for argument 1 from ‘Cell*’ to ‘const Cell&amp;’
  </code></pre>
  
  <p>Line 39 is where I set <code>nwArray[i][j] = new Cell(10, 'q')</code>. So I introduce the overloaded assignment operator and got a similar error:</p>
  
  <pre><code>Cell&amp; Cell::operator=(const Cell&amp; other){
    if(this == &amp;other)
      return *this;
    score = other.score;
    parent = other.parent;
    return *this;
  }
  
  g++ test.cpp -o test                                             │ 12     char getParent();$                                                                     
  test.cpp: In function ‘int main()’:                                                             │ 13 };$                                                                                        
  test.cpp:48:39: error: no match for ‘operator=’ in ‘*((*(nwArray + ((sizetype)(((unsigned int)i)│ 14 $                                                                                          
   * 4u)))) + ((sizetype)(((unsigned int)j) * 8u))) = (operator new(8u), (&lt;statement&gt;, ((Cell*)&lt;an│ 15 Cell::Cell(){$                                                                             
  onymous&gt;)))’                                                                                    │ 16   score = 0;$                                                                              
  test.cpp:48:39: note: candidate is:                                                             │ 17   parent = '-';$                                                                           
  test.cpp:25:7: note: Cell&amp; Cell::operator=(const Cell&amp;)                                         │ 18 }$                                                                                         
  test.cpp:25:7: note:   no known conversion for argument 1 from ‘Cell*’ to ‘const Cell&amp;’
  </code></pre>
  
  <p>My question is why doesn't this work? The overloaded assignment operator should return an address to a cell thus making it valid to store it in a pointer within the array. What am I missing here? </p>