I'm new to pointers and having a hard time. I'm doin a predator-prey simulator and have a City class that has an array of Being objects, which are eventually to be cast as either Humans or Zombies. City class has an array:
class City
{
protected:
Being *grid[GRID_HEIGHT][GRID_WIDTH];//holds beings
I initialize the elements to null to start with just to keep it simple.
City::City()
{
for (int i = 0; i < GRID_HEIGHT; ++i) {
for (int j = 0; j < GRID_WIDTH; ++j) {
this->grid[i][j] = NULL;
}
}
}
I also have a method that should return a single Being from the array. THIS is where I am getting errors: "identifier 'grid' is undefined" or if I use the "this" keyword it says that's only allowed in a nonstatic member function.
Being City::*getBeing(int x, int y)
{
return grid[x][y];
}
so I can access it from the main method and just print a placeholder grid:
City myCity;
int main(){
if (myCity.getBeing(i,j) == null)
{
cout << "O";
}
}
How can I fix this? Is there a rule of thumb for understanding pointers better?