2013-11-03 44 views
3

我得到了网格的默认构造函数的前两行编译器错误“错误:墙不是网格的成员”。我不知道它为什么说我在我的头文件中定义了墙和网格!我也尝试过使用this-> wall,Grid :: wall和一个初始化列表。下面的代码:编译器说变量不是类的成员,当它是

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

我得到当我改变我的默认构造函数使用初始化列表相同的错误:

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

这里是休息我的编译器错误(要求在评论中)。对不起格式化,出于某种原因,我的电脑吐出怪异的字符。

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 
+2

使用初始化列表并在构造函数之前定义您的类。 –

+0

后半部分在我的h文件中。这些代码片段,我将编辑清晰 – zaloo

+1

你有任何其他错误?难道你没有在'Species'类被定义的头部包含头部吗? – juanchopanza

回答

3

“具有不完全类型”是指你没有给编译器的Species定义。没有定义,最多可以有指向数据的指针,因为编译器不知道需要预留多少空间。所以它给出了一个错误,然后忽略了这一行,并试图理解程序的其余部分。当然,因为该行被忽略了,以后尝试使用它将会失败。

请注意,您的编辑器通过文件名排序了错误,而不是显示它们实际发生的顺序。将来,请按顺序查看编译器输出。

通过在class Grid之前加上Species的定义(或#include),可以很容易地解决这个问题。

+0

嘿,工作! – zaloo

-1

您正在使用static(或类)变量的语法,但这些是实例变量。试试这个

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

+0

虽然资格在这里肯定没有必要,但它[也应该工作](http://ideone.com/I3iZ1e)。有时使用限定名称作为实例成员是必要的,例如,调用重写的成员函数的基本版本。 –

+0

无论如何,'this->'也是没有必要的。 –

+0

您应该在这里使用构造函数初始化列表。我已经表明OP做到这一点,不会他们会感到困惑,并养成坏习惯:) – juanchopanza

相关问题