简体   繁体   中英

Should I overload the assignment operator C++?

I have the following code:

#include <iostream>

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 < 10; i++){
    nwArray[i] = new Cell[10];
  }
  for(int i = 0; i < 10; i++){
    for(int j = 0; j < 10; j++){
      nwArray[i][j] = new Cell(10, 'q');
      std::cout << nwArray[i][j].getScore() << "\t";
    }
  }
}

Compilation results in the following:

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), (<statement>, ((Cell*)<anonymous>)))’
test.cpp:39:39: note: candidate is:
test.cpp:3:7: note: Cell& Cell::operator=(const Cell&)
test.cpp:3:7: note:   no known conversion for argument 1 from ‘Cell*’ to ‘const Cell&’

Line 39 is where I set nwArray[i][j] = new Cell(10, 'q') . So I introduce the overloaded assignment operator and got a similar error:

Cell& Cell::operator=(const Cell& other){
  if(this == &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), (<statement>, ((Cell*)<an│ 15 Cell::Cell(){$                                                                             
onymous>)))’                                                                                    │ 16   score = 0;$                                                                              
test.cpp:48:39: note: candidate is:                                                             │ 17   parent = '-';$                                                                           
test.cpp:25:7: note: Cell& Cell::operator=(const Cell&)                                         │ 18 }$                                                                                         
test.cpp:25:7: note:   no known conversion for argument 1 from ‘Cell*’ to ‘const Cell&’

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?

nwArray[i][j] has type Cell

new Cell(10, 'q') has type Cell *

I hope now you understand why the compiler issue an array for statement

nwArray[i][j] = new Cell(10, 'q');

. No copy assignment operator is required because there is no any problem with the implicitly defined copy assignment operator

Maybe you should define nwArray

Cell*** nwArray;

that to esacpe the error if you want to have a multidimensional array of type pointer to Cell

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM