1

したがって、私の現在の任務は、基本的なハンターと獲物のシミュレーションを作成することです。他にもいくつかの問題があった後、教授は、今のところmain.cpp実際に機能させるためにすべてを投入するようにアドバイスしました。

私の現在の問題は、関数内で、ファイルの先頭で事前に宣言されているにもかかわらずCreature::Find()、クラスが不完全であると主張していることです。Grid私の最初の考えは、Gridクラスを の前に配置することでしたが、 は本質的にポインターの 2D 配列であるCreatureため、これにより多くのまたは同じエラー (Creature不完全であることを参照) が発生します。以下はコードの関連部分です。ファイル全体は Dropbox で見つけることができますGridCreature


class Grid;  //*** error: forward declaration of 'class Grid'

//Other stuff...

class Creature
{
public:
    Grid* theGrid;
    Coords position;
    int stepBreed;
    int stepHunger;
    char face;
    bool hasMoved;

    Creature(Grid* _grid, Coords _position, char _face)  //Constructor
    {
        theGrid = _grid;
        position = _position;
        face = _face;
        stepBreed = stepHunger = 0;
        hasMoved = false;
    }

    vector<Coords> Find(char thisFace)  //Returns a list of coords with prey on them
    {
        vector<Coords> result;
        for(int i = position.x-1; i <= position.x+1; i++)
            for(int j = position.y-1; j <= position.y+1; j++)
            {
                Coords temp(i,j);
                if(theGrid->Occupant(temp) == thisFace) //*** error: invalid use of incomplete type 'class Grid'
                    result.push_back(temp);
            }
        return result;
    }

    virtual Coords Move() = 0;  //Allows the creature type to define it's own movement
    virtual Coords Breed() = 0;  //Allows the creature type to define it's own breeding
    virtual bool Starve() = 0;  //Allows the creature type to starve of its own accord
};

class Grid
{
public:
    Creature* grid[MAX_X][MAX_Y];

    Grid() //Initalizes the grid and spawns random creatures
    {
        cout<<endl<<"grid init"<<endl;
        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
    {
        cout<<endl<<"---  Grid::Move() TOP  ---"<<endl<<endl;
        ResetMoved();

        for(int i = 0; i < MAX_X; i++)
            for(int j = 0; j < MAX_Y; j++)
                if(grid[i][j])
                    grid[i][j]->Move();
        cout<<endl<<"---  Grid::Move() BOTTOM  ---"<<endl<<endl;
    }

    void Breed() //Tells each creature to breed (if it can)
    {

    }

    void Kill()  //Tells each creature to die (if it's old)
    {

    }

    char** Snapshot()  //Creates a char array "snapshot" of the board
    {
        char** result = new char*[MAX_X];
        for(int i = 0; i < MAX_X; i++)
        {
            result[i] = new char[MAX_Y];
            for(int j = 0; j < MAX_Y; j++)
            {
                result[i][j] = Occupant(Coords(i, j));
            }
        }
        return result;
    }

    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;
        return Get(here)->face;
    }

    void Clear(Coords here)  //Deletes the object at the specified position
    {
        cout<<endl<<"---  Grid::Clear() TOP  ---"<<endl<<endl;

        if(!Get(here))
        {
            cout<<"   inside if"<<endl;
            delete Get(here);
        }
        cout<<"   outside if"<<endl;
        grid[here.x][here.y] = NULL;

        cout<<endl<<"---  Grid::Clear() BOTTOM  ---"<<endl<<endl;
    }

    void ResetMoved()
    {
        for(int i = 0; i < MAX_X; i++)
            for(int j = 0; j < MAX_Y; j++)
                if(grid[i][j])
                    grid[i][j]->hasMoved = false;
    }
};

編集: プレビューとマークアップのツールバーが何らかの理由で機能していません。

4

2 に答える 2

3

循環依存関係があります (以前にこの質問をしたことを覚えているようです)。同じファイルに何かを入れても、実際には問題は解決しません (ただし、問題をもう少し明確に理解するのに役立つかもしれません)。あなたがしなければならないことは、すべての関数が必要なクラスの後に定義されるように、物事を正しく並べることです。

この場合、私はこのようにします

class Grid;

class Creature
{
public:
    Grid* theGrid;
    ...
    vector<Coords> Find(char thisFace);
    ...
};

class Grid
{
public:
    Creature* grid[MAX_X][MAX_Y];
    ...
};

vector<Coords> Creature::Find(char thisFace)
{
   ...
}

Creature::FindCreatureクラス (明らかに) とクラスの両方が必要なGridので、両方のクラスが完全に定義された後に実行する必要があります。

Creature::Find定義をヘッダー ファイルに配置することになった場合は、キーワードを追加する必要がありますinline。そうしないと、複数の定義が取得されます。

于 2013-04-20T06:51:31.200 に答える
0

ポインタの宣言は前方宣言で十分です。ただし、コンパイラは、前方宣言されたクラスに何が含まれているかをまだ知りません。したがって、次のようなことを許可することは期待できません

class Grid;
//
theGrid->Occupant(temp)

グリッドの定義全体を含むファイルを含める必要があります。または、関数の定義をCreature::findGrid.h が含まれる別のファイルに移動できます。たとえば、Grid.cpp

于 2013-04-20T06:52:12.150 に答える