gpt4 book ai didi

c++ - 使用时 0xcdcdcdcd->

转载 作者:行者123 更新时间:2023-11-30 05:28:43 29 4
gpt4 key购买 nike

嗨,我正在为我的学校做一个小项目,并且对我的错误越来越奇怪。

在我的对象中调用其中一个方法时,此指针设置为 0xcdcdcdcd。我用谷歌搜索并找到了一些关于在调用之前删除内存或销毁对象的信息,但我确保之前没有调用析构函数。

世界.h

class Organism;
class Human;

class World
{
private:
vector <Organism*> organisms;

vector <Organism*> organismsToAdd;

vector <string> logs;

int turn_;

void initializeWorld();

void drawInterface();

void drawInfo();
void drawOrganisms();
void nextTurn();
bool isPositionTaken(int x, int y);
Organism* getOrganism(int x, int y);
void queueOrganismToAdd(Organism* newOrganism);
void addQueuedOrganisms();
void generateStartOrganisms();
bool isPlayerAlive();
public:
void executeMove(Organism* moving, int toX, int toY); //here's the problem
bool isPositionValid(int x, int y);
World(int x, int y);
struct
{
int x_, y_;
} worldSize;
void startGame();
~World();
};

执行移动

void World::executeMove(Organism* moving, int toX, int toY)
{
cout << moving->getSign();
getch();
if (!isPositionTaken(toX, toY)) // <- here it brake
{
moving->setPosition(toX, toY);
}
else if (moving->getSign() == getOrganism(toX, toY)->getSign())
{
//multiply
//make log
}
else {
if (!moving->specialCollision((getOrganism(toX, toY)))) return;
if (!getOrganism(toX, toY)->specialCollision(moving)) return;
if (moving->getPower() >= getOrganism(toX, toY)->getPower())
{
//log
//delete losser
}
else
{
//log
//delete losser
}
moving->setPosition(toX, toY);
}
}

已定位

bool World::isPositionTaken(int x, int y)
{
for (int i = 0; i < this->organisms.size(); ++i) // here this is set to 0xcdcdcdcd
{
if (organisms[i]->getPositionX() == x && organisms[i]->getPositionY() == y) return true;

}
return false;
}

方法 isPositionTaken 在项目的其他部分运行良好,所以如果发现错误,我会完全迷失,我感谢任何帮助

最佳答案

由于 organisms 成员具有默认构造函数,因此在您指定的行中查看此行为的唯一方法是调用 executeMove() 时使用的是未初始化的指针。

类似于:

World *ptr; // not initialized on stack
...
ptr->executeMove();

或者这个方法是从另一个有同样问题的方法调用的。

关于c++ - 使用时 0xcdcdcdcd->,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36672154/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com