我应该重载赋值运算符C ++吗?

时间:2014-01-14 04:19:59

标签: c++ arrays pointers operator-overloading

我有以下代码:

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

我的问题是为什么这不起作用?重载赋值运算符应该向单元格返回一个地址,从而使其有效地将其存储在数组中的指针中。我在这里错过了什么?

1 个答案:

答案 0 :(得分:2)

nwArray [i] [j]的类型为Cell

new Cell(10,'q')的类型为Cell *

我希望您现在了解编译器为语句

发出数组的原因
nwArray[i][j] = new Cell(10, 'q');

。不需要复制赋值运算符,因为隐式定义的复制赋值运算符

没有任何问题

也许你应该定义nwArray

Cell*** nwArray;

如果你想拥有一个指向Cell的类型指针的多维数组

,那就要解决错误