编译器说变量不是类的成员 [英] Compiler saying variable is not a member of class when it is

查看:309
本文介绍了编译器说变量不是类的成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在网格的默认构造函数的前两行出现编译器错误错误:wall不是Grid的成员。我不确定为什么要这样说,因为我在头文件中同时定义了墙和网格!我还尝试过使用this-> wall,Grid :: wall和初始化列表。代码如下:

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;
        }
}

在标头文件中:

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


推荐答案

具有不完整的类型类型表示您没有为编译器提供 Species 的定义。如果没有定义,则最多只能有指向数据的指针,因为编译器不知道要保留多少空间。因此,它给出一个错误,然后忽略该行,并尝试使程序的其余部分有意义。当然,因为忽略了该行,以后尝试使用它会失败。

"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.

应该通过放置 Species的定义(或#include)来轻松解决所有问题。 放在类网格之前。

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

这篇关于编译器说变量不是类的成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆