0

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?

1 Answer 1

3

Definition of the getBeing function should be:

Being *City::getBeing(int x, int y) { ... }
Sign up to request clarification or add additional context in comments.

2 Comments

It was that easy. Also for future reference, I also changed the NULL initializer to 0 as I had an error in the main class of "identifier null is undefined". Thanks!
it should be NULL, not null. If you are using C++11 I strongly suggest you use nullptr instead!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.