1

所以我目前的任务是做一个基本的猎人和猎物模拟,在遇到其他几个问题后,我的教授建议我把所有东西都放进main.cpp去,让这个东西现在可以正常工作。

我当前的问题是,在Creature::Find()函数中它声称Grid该类是不完整的,即使它已在文件顶部预先声明。我最初的想法是将Grid类放在之前Creature,但这会导致更多或相同的错误(指的Creature是不完整),因为Grid本质上是一个二维Creature指针数组。下面是代码的相关部分,整个文件可以在 Dropbox 上找到


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::Find需要Creature类(显然)和Grid类,所以它必须在两个类都被完全定义之后。

如果您最终将Creature::Find定义放在头文件中,则必须添加inline关键字,否则您将获得多个定义。

于 2013-04-20T06:51:31.200 回答
0

前向声明适用于声明指针。但是编译器还不知道您的前向声明的类包含什么。所以你不能指望它允许像

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

您需要包含包含 Grid 的整个定义的文件。或者,您可以将Creature::find函数的定义移动到包含 Grid.h 的另一个文件中。例如在Grid.cpp

于 2013-04-20T06:52:12.150 回答