简体   繁体   English

生命游戏,C ++-多态数组(可能还有指针)

[英]Game of Life, C++ - Polymorphic Array (And maybe Pointers)

I'm having trouble with some code, dealing with polymorphism. 我在处理多态性的某些代码时遇到麻烦。

Essentially I am to make a Game of Life, and as such have a driver where I create a grid of Organism pointers (the super class to actual animal classes, in this case, Ant). 从本质上讲,我是要做一个“人生游戏”,因此要有一个驱动程序,在其中创建一个有机体指针网格(实际动物类的超类,在这种情况下为Ant)。

In my driver, I create a new Ant in the array of Organism pointers using the following code: 在驱动程序中,我使用以下代码在有机体指针数组中创建一个新的Ant:

[DRIVER]
Ant *ant = new Ant(&world, GRID_HEIGHT, GRID_WIDTH);
world.setOrganism(ant,4,4); 

Here I create a pointer to an Ant, and plop it in the world. 在这里,我创建了一个指向蚂蚁的指针,并将其放到世界中。 Here's setOrgamism function: 这是setOrgamism函数:

[World]
void World::setOrganism(Organism *organism, int x, int y)
{   grid[x][y] = organism; }

So, this code works when called from the driver. 因此,从驱动程序调用时,此代码有效。 I can make a new ant using this anywhere, no problems. 我可以在任何地方使用这种新蚂蚁,没有问题。

However, when I try to run this in the Ant's spawn function, I get an Access Violation error and my program crashes executing setOrganism. 但是,当我尝试在Ant的spawn函数中运行此命令时,出现访问冲突错误,并且我的程序在执行setOrganism时崩溃。

Here's the code in my Ant class for the spawn function: 这是我的Ant类中用于spawn函数的代码:

[Ant]
void Ant::spawn(int spawnX, int spawnY)
{
     Ant *newAnt = new Ant(world, GRID_HEIGHT, GRID_WIDTH);
     newAnt->setPosition(spawnX,spawnY);

     world->setOrganism(newAnt, spawnX, spawnY);
}

Any and all help is certainly appreciated! 任何帮助都将不胜感激!

Edit: 编辑:

[World.cpp]
#include "World.h"
#include "Ant.h"
#include "Lion.h"

using namespace std;

World::~World(void)
{
}

World::World(void)
{
    for(int y = 0; y < GRID_HEIGHT; y++)
    {
        for(int x = 0; x < GRID_WIDTH; x++)
        {
            grid[x][y] = NULL;
        }
    }
}

Organism* World::getOrganism(int x, int y)
{
    return grid[x][y];
}

void World::setOrganism(Organism *organism, int x, int y)
{
    grid[x][y] = organism;
    organism->setPosition(x,y);
}

_ _

[Ant]
#include "Ant.h"

using namespace std;

Ant::~Ant(void)
{
}

Ant::Ant(void)
{
}

Ant::Ant(World *world, int width, int height)
{
    srand(time(NULL));
    this->width = width;
    this->height = height;
}

void Ant::spawn(int spawnX, int spawnY)
{
    Ant *newAnt = new Ant(world, GRID_HEIGHT, GRID_WIDTH);
    newAnt->setPosition(spawnX,spawnY);

    world->setOrganism(newAnt, spawnX, spawnY);
}

_ _

[Driver]
#include "Organism.h"
#include "Ant.h"
#include "Lion.h"
#include "World.h"

using namespace std;

int main()
{

    World *world = new World();

    //This works
    Ant *ant = new Ant(world, GRID_HEIGHT, GRID_WIDTH);
    world->setOrganism(ant,4,4);

    //This does not
    ant->spawn(6,6);
}

If you need any more information, please ask! 如果您需要更多信息,请询问!

You haven't shown the definition of Organism , but from your comments, I assume it has a member World *world . 您尚未显示Organism的定义,但是从您的评论中,我认为它具有成员World *world You're passing a World *world into the constructor of Ant , but you're not using it for anything; 您正在将World *world传递给Ant的构造函数,但是您没有将其用于任何东西; you're not assigning it anywhere, and you're not calling any non-default constructor of Organism either. 您不会在任何地方分配它,也不会调用任何非默认的Organism构造函数。 This means that the member world is most probably left uninitialised. 这意味着成员world很可能未初始化。

This in turn means that inside Ant::spawn , the world (referring to this->world ) is either a null pointer or a garbage value (depending on how the default constructor of Organism initialises its world member). 反过来,这意味着在Ant::spawn内部, world (指this->world )是空指针或垃圾值(取决于Organism的默认构造函数初始化其world成员的方式)。 Either of these will lead to an Access violation when dereferenced - precisely what you're getting. 取消引用时,这两种都会导致访问冲突-正是您所获得的。

To fix that, make sure Ant 's constructor initialises the world member somehow - probably by passing on the world parameter to the appropriate constructor of Organism . 要解决此问题,请确保Ant的构造函数以某种方式初始化world成员-可能是通过将world参数传递给适当的Organism构造函数。

you didn't declare the type of grid. 您没有声明网格的类型。 you just have grid[x][y] = null; 您只有grid [x] [y] = null; you should have set it to Organism grid[x][y], and then set it later on 您应该将其设置为“有机体网格” [x] [y],然后稍后将其设置为

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM