I am having an issue when trying to populate a 2D array full of objects of a class I have created. The error is:
error C2679: binary '=' : no operator found which takes a right-hand operand of type 'Cell *' (or there is no acceptable conversion)
The code that generates the error is as follows:
Excerpt from main.cpp
Cell cells[80][72];
for(int x = 0; x < 80; x++){
for(int y = 0; y < 72; y++){
cells[x][y] = new Cell();
}
}
Excerpt from cell.hpp
class Cell
{
public:
Cell();
int live;
int neighbours;
};
Excerpt from cell.cpp
Cell::Cell()
{
srand(time(0));
this->live = rand() % 2;
this->neighbours = 0;
}
I suspect that I need an overload of some kind on the constructor of the Cell class but I have no idea how to implement one for this case.
Since
Cellhas a parameterless constructor the statementdefines an 80×72 array of
Cellobjects. The objects are already constructed for you so the nestedforloops are unnecessary.On the other hand if you declare
Cellas a 80×72 array of pointers toCell, i.e.Then you have to go allocate each one like you’re trying to do.
If you don’t actually need to use pointers, then don’t.