Compiler saying variable is not a member of class

2019-07-04 04:24发布

问题:

I'm getting the compiler error "error: wall is not a member of Grid" on the first two lines of the default constructor for grid. I'm not sure why it is saying that as I defined both wall and grid in my header file! I've also tried using this->wall, Grid::wall and an initialization list. Here's the code:

Grid::Grid() {
    this->wall = Species("wall");
    this->empty = Species("empty");
    Grid::turn_number = 0;
    int a,b;
    for(a= 0; a < 100; a++)
        for(b = 0; b< 100; b++) {
            Creature empty_creature = Creature(Grid::empty,a,b,NORTH,this);
            ((Grid::map)[a][b]) = empty_creature;
        }
    Grid::width = 0;
    Grid::height = 0;
}

I get the same error when I change my default constructor to use an initialization list:

Grid::Grid()
: width(0), height(0), turn_number(0), wall("wall"), empty("empty"){
    int a,b;
    for(a= 0; a < 100; a++)
        for(b = 0; b< 100; b++) {
            Creature empty_creature = Creature(Grid::empty,a,b,NORTH,this);
            ((Grid::map)[a][b]) = empty_creature;
        }
}

In the Header file:

class Grid {
protected:
    Creature map[100][100];
    int width,height;
    int turn_number;
    Species empty;
    Species wall;
public:
    Grid();
    Grid(int _width, int _height);
    void addCreature(Species &_species, int x, int y, Direction orientation);
    void addWall(int x, int y);
    void takeTurn();
    void infect(int x, int y, Direction orientation, Species &_species);
    void hop(int x, int y, Direction orientation);
    bool ifWall(int x, int y, Direction orientation);
    bool ifEnemy(int x, int y, Direction orientation, Species &_species);
    bool ifEmpty(int x, int y, Direction orientation);
    void print();
};

Here's the rest of my compiler errors (asked for in comments). Sorry for the formatting, my computer spits out weird characters for some reason.

Darwin.c++: In constructor ‘Grid::Grid()’:
Darwin.c++:8:40: error: class ‘Grid’ does not have any field named ‘wall’
Darwin.c++:8:54: error: class ‘Grid’ does not have any field named ‘empty’
Darwin.c++:12:39: error: ‘empty’ is not a member of ‘Grid’
Darwin.c++: In constructor ‘Grid::Grid(int, int)’:
Darwin.c++:17:86: error: class ‘Grid’ does not have any field named ‘wall’
Darwin.c++:17:99: error: class ‘Grid’ does not have any field named ‘empty’
Darwin.c++:21:39: error: ‘empty’ is not a member of ‘Grid’
Darwin.c++: In member function ‘void Grid::addWall(int, int)’:
Darwin.c++:32:31: error: ‘wall’ is not a member of ‘Grid’
Darwin.h:35:10: error: field ‘empty’ has incomplete type
Darwin.h:36:10: error: field ‘wall’ has incomplete type
In file included from RunDarwin.c++:33:0:
Darwin.h:35:10: error: field ‘empty’ has incomplete type
Darwin.h:36:10: error: field ‘wall’ has incomplete type

回答1:

"has incomplete type" means you haven't given the compiler the definition of Species. Without the definition, at most you can have pointers to the data, because the compiler doesn't know how much space to reserve. So it gives an error, and then ignores the line and tries to make sense of the rest of the program. Of course, because the line was ignored, trying to use it later will fail.

Note that your editor has sorted the errors by filename instead of showing you the order they actually occurred. In the future, look at the compiler output in order.

This should all be easily fixed by putting the definition (or #include) for Species before class Grid.



回答2:

You're using the syntax for static (or class) variables, but these are instance variables. Try this

Grid::Grid() {
    this->wall = Species("wall");
    this->empty = Species("empty");
    this->turn_number = 0;

etc.