3

グリッドのデフォルト コンストラクターの最初の 2 行で、"エラー: 壁はグリッドのメンバーではありません" というコンパイラ エラーが発生します。ヘッダー ファイルで wall と grid の両方を定義したので、なぜそう言っているのかわかりません! 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
4

2 に答える 2

3

「不完全な型があります」とは、コンパイラに の定義を与えていないことを意味しますSpecies。定義がなければ、コンパイラーは予約するスペースを知らないため、データへのポインターしか持つことができません。そのため、エラーが発生し、その行を無視してプログラムの残りの部分を理解しようとします。もちろん、その行は無視されたので、後で使用しようとすると失敗します。

エディターは、エラーが実際に発生した順序ではなく、ファイル名で並べ替えていることに注意してください。今後、コンパイラの出力を順番に見ていきます。

Speciesこれはすべて、 beforeの定義 (または #include) を配置することで簡単に修正できますclass Grid

于 2013-11-03T22:40:52.830 に答える