簡體   English   中英

我應該重載賦值運算符C ++嗎?

[英]Should I overload the assignment operator C++?

我有以下代碼:

#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";
    }
  }
}

編譯結果如下:

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&’

第39行是我設置nwArray[i][j] = new Cell(10, 'q') 所以我介紹了重載賦值運算符並得到了類似的錯誤:

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&’

我的問題是為什么這不起作用? 重載賦值運算符應該向單元格返回一個地址,從而使其有效地將其存儲在數組中的指針中。 我在這里錯過了什么?

nwArray [i] [j]有類型Cell

新Cell(10,'q')有類型Cell *

我希望你現在理解為什么編譯器為語句發出一個數組

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

不需要復制賦值運算符,因為隱式定義的復制賦值運算符沒有任何問題

也許你應該定義nwArray

Cell*** nwArray;

如果你想擁有一個指向Cell的類型指針的多維數組,那就要解決錯誤

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM