単純なハンター/獲物のシミュレーションで奇妙なコンパイル エラーがあちこちで発生し (主に、教授が継承されたクラスと仮想関数の構文を十分に説明していないため)、1 つの問題で完全に行き詰まっています。Move()
このプログラムでは、「クリーチャー」(「ハンター」と「プレイ」の子を持つ抽象クラス) が「グリッド」クラスを、Breed()
、Die()
サイクルで歩き回ります。
次のエラーが表示されます: 「C2027: 未定義の型 'Creature' の使用」および「C2227: '->face' の左は class/struct/union/generic 型を指している必要があります」 以下で指定された行 (すべて何人かの学生が別のプロジェクトで未解決の外部を取得していて、教授がすべてをヘッダーに入れるように言ったので、私のコードはヘッダーにあります)。さらにコードを投稿する必要がある場合はお知らせください。
これまで説明できなかった他のいくつかのエラーが発生しましたが、これらは、含まれているヘッダーの追加/削除とクラスの事前宣言の一見ランダムな組み合わせによって解決されたように見えましたが、何が問題なのかについての実際の説明をいただければ幸いです。それが機能するまで、私はただ暗闇の中でぶらぶらしているわけではありません。私たちがやろうとしていることの概念と、その大部分の実行方法については理解していますが、前述したように、複数のファイルを適切にセットアップする方法の構文には時間を費やしていませんでした。スムーズに動作するため、これをどのように行うべきかについての詳細な説明をいただければ幸いです。
Grid.h
#ifndef GRID_H
#define GRID_H
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include "Constants.h"
#include "creature.h"
using namespace std;
class Creature;
class Grid
{
public:
Creature* grid[MAX_X][MAX_Y];
Grid() //Initalizes the grid and spawns random creatures
{
for(int i = 0; i < MAX_X; i++)
for(int j = 0; j < MAX_Y; j++)
grid[i][j] = NULL;
}
void Move() //Tells each creature on the grid to move
{
//Call creature.Move() on each cell (if not NULL)
}
void Breed() //Tells each creature to breed (if it can)
{
//Call creature.Breed() on each cell (if not NULL)
}
void Kill() //Tells each creature to die (if it's old)
{
//Call creature.Die() on each cell (if not NULL)
}
char** Snapshot() //Creates a char array "snapshot" of the board
{
//Produces a 2D char array of the grid for display
}
Creature* Get(Coords here) //Returns a pointer to the object at the specified position
{
return grid[here.x][here.y];
}
char Occupant(Coords here) //Returns the character of the specified position
{
if(!Get(here))
return FACE_EMPTY;
Creature* temp = Get(here);
return temp->face; //*** ERRORS APPEAR HERE ***
}
void Clear(Coords here) //Deletes the object at the specified position
{
if(Get(here))
delete Get(here);
grid[here.x][here.y] = NULL;
}
};
#endif // GRID_H
クリーチャー.h
#ifndef CREATURE_H
#define CREATURE_H
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include "Constants.h"
#include "coords.h"
#include "grid.h"
using namespace std;
class Grid;
class Creature
{
public:
Grid* theGrid;
Coords position;
int stepBreed;
int stepHunger;
char face;
Creature(Grid* _grid, Coords _position, char _face) //Constructor
{
theGrid = _grid;
position = _position;
face = _face;
stepBreed = stepHunger = 0;
}
virtual Coords Move() = 0; //Allows the child to define it's own movement
virtual Coords Breed() = 0; //Allows the child to define it's own breeding
virtual bool Starve() = 0; //Allows the child to starve of its own accord
};
#endif // CREATURE_H