使用此>时0xcdcdcdcd

0xcdcdcdcd while using this->

本文关键字:0xcdcdcdcd gt      更新时间:2023-10-16

嗨,我在学校做一个小项目,一直在犯奇怪的错误。

当调用我的对象中的一个方法时,该指针被设置为0xcdcdcd。我在谷歌上搜索了一下,发现了一些关于在调用之前擦除内存或销毁对象的信息,但我确保之前没有调用析构函数。

World.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);
   }
}

is Position in Take

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();

或者此方法是从具有相同问题的另一个方法调用的。