c++ - 指针值在分配后不久重置为空

标签 c++ pointers

我有一个指针设置为 0 ,然后在同一个函数中,在一些循环/条件中我尝试重新分配它..(请阅读评论)

for(Entity* tile : _originalFloorTiles)
        {
            for(Turns turn : pointsUpLeftDownRight)
            {
                if(tile->GetCollisionRect().ContainsPoint(turn.first.x, turn.first.y)){
                    turn.second = tile; //everything looks fine here, turn.second is still null and tile is a valid pointer
                    assert(turn.second); //turn.second is definitely assigned the value of tile here.
                }
               HAPI->DebugText("check pointsUpLeftDownRight now");//!Here's where it gets weird,
            // If i hover over turn and inspect it in visual studio now, turn.second is still completely valid 
            // (with the assigned value of tile).
            // But hovering over pointsUpLeftDownRight shows its contents for each turn..
            // and inside there the current turn is a NULL pointer for turn.second!
            }
        }

所以前一刻我已经分配我的指针没有问题,下一刻指针似乎根本没有改变。

为了澄清,Turnsstd::pair<Vect, Entity*> 的惰性 typedef ,如果这让我的代码更难阅读,我深表歉意,它是一些快速拼凑的敌人 ai。我将在下面发布完整的功能。

我真的被难住了,不确定我是不是个白痴或发生了什么奇怪的事情,真的很感谢任何人花时间看看。

//寻找幽灵可以进行的回合。

void IceGhostNPC::RespondToTimePassed()
{   
    //Entity* t = _originalFloorTiles[0];

    //test if enough time has passed since ghost last decided to look for turns
    if(_lastTimeTurned < timeGetTime() - _timeBeforeSearchingForTurns)
    {
        //store points surrounding ghost in a way that we can associate them with a valid floor tile to move onto 
        std::vector<Turns> pointsUpLeftDownRight;
        pointsUpLeftDownRight.push_back(
            Turns(Vect(GetCenterXPos(), GetCenterYPos() - floorTileHeight), 0)); //point above
        pointsUpLeftDownRight.push_back(
            Turns(Vect(GetCenterXPos() - floorTileWidth, GetCenterYPos()), 0)); //point left
        pointsUpLeftDownRight.push_back(
            Turns(Vect(GetCenterXPos(), GetCenterYPos() + floorTileHeight), 0)); //point down
        pointsUpLeftDownRight.push_back(
            Turns(Vect(GetCenterXPos() + floorTileWidth, GetCenterYPos()), 0)); //point right

        //look through original floor tiles, 
        for(Entity* tile : _originalFloorTiles)
        {
            //see if its possible to take a turn 
            for(Turns turn : pointsUpLeftDownRight)
            {
                if(tile->GetCollisionRect().ContainsPoint(turn.first.x, turn.first.y)){
                    turn.second = tile;
                    assert(turn.second);
                }
                HAPI->DebugText("check pointsUpLeftDownRight now");
            }
        }

        //Now to make the behaviour more interesting we have the ghost randomly take one of the turns,
        // ( we use associated tile to check the turn is possible, and we can also change that tile to an icy patch )
        bool turnTaken = false;
        do{
            int turnChoice = rand() % 4;
            if(pointsUpLeftDownRight[turnChoice].second == 0)
                continue; //go back to top of loop if that turn had a null tile
            else
            {
                switch(turnChoice){
                    case 0: //turn upwards
                        _moveable->SetYDirection(Controller::UP);
                        _moveable->SetXDirection(Controller::NONE);
                        break;
                    case 1: //turn leftwards
                        _moveable->SetYDirection(Controller::NONE);
                        _moveable->SetXDirection(Controller::LEFT);
                        break;
                    case 2: //turn downwards
                        _moveable->SetYDirection(Controller::DOWN);
                        _moveable->SetXDirection(Controller::NONE);
                        break;
                    case 3: //turn right
                        _moveable->SetYDirection(Controller::NONE);
                        _moveable->SetXDirection(Controller::RIGHT);
                        break;
                }
                turnTaken = true;
                _lastTimeTurned = timeGetTime();
                //ice tile up baby
            }

        }while(turnTaken = false);
    }

    FinishResponding(timeGetTime());
}

最佳答案

检查这一行:

for(Turns turn : pointsUpLeftDownRight)

您正在遍历 pointsUpLeftDownRight 中元素的拷贝。当拷贝被销毁时(在 for 主体的末尾),您分配给该拷贝的任何值都将丢失。您的任务临时更改。

试试这个:

for(Turns& turn : pointsUpLeftDownRight)

关于c++ - 指针值在分配后不久重置为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13958726/

相关文章:

c - C中带符号数的数组索引

c - "Error: expected expression before ‘int’ "and "错误 : expected ‘;’ , ‘,’ 或 ‘)’ 之前 ‘int’ "

c++ - 成员函数不继承?

c++ - 条件评估是否优化?这段代码不好吗?

c++ - 将指针设置为 null 会使我的 C++ 程序崩溃

C++ : get inherited type from abstract pointer vector

c++ - QTabWidget - 如何将 "include"指向每个选项卡的指针?

c++ - 如何将模板函数的所有参数隐式转换为最高分辨率类型?

c++ - 如何将文件中的字符串和整数放入多维数组?

c++ - 如何使用 C++ 运行批处理文件?