C++ 为什么这个节点列表不存在';工作不正常吗?

C++ 为什么这个节点列表不存在';工作不正常吗?,c++,list,C++,List,为什么,虽然我添加了4个游戏对象,但在GameLoop中,控制台只提供一次“游戏对象更新”和“游戏对象渲染”输出 还有一个问题,我如何为游戏对象制作一个自我毁灭功能 最后一个问题,一个游戏对象可以与列表中的其他游戏对象通信的最佳方法是什么 #include <iostream> using namespace std; class GameObject { public: GameObject *nextGameObject; GameObject() {

为什么,虽然我添加了4个游戏对象,但在GameLoop中,控制台只提供一次“游戏对象更新”和“游戏对象渲染”输出

还有一个问题,我如何为游戏对象制作一个自我毁灭功能

最后一个问题,一个游戏对象可以与列表中的其他游戏对象通信的最佳方法是什么

#include <iostream>
using namespace std;

class GameObject
{
public:
    GameObject *nextGameObject;

    GameObject()
    {
        cout<<"GameObject Constructor!\n";
        nextGameObject = nullptr;
    }
    ~GameObject()
    {
        cout<<"GameObject Destructor\n";
        if(nextGameObject != nullptr)
        {
            delete nextGameObject;
        }
    }

    virtual void Update()
    {
        cout<<"GameObject Update!\n";
    }
    virtual void Render()
    {
        cout<<"GameObject Render!\n";
    }
};

class GameObjectManager
{
private:
    GameObject *firstGameObject;
public:
    GameObjectManager()
    {
        firstGameObject = nullptr;
    }
    ~GameObjectManager()
    {
        if(firstGameObject != nullptr)
        {
            delete firstGameObject;
        }
    }

    void Update()
    {
        if(firstGameObject != nullptr)
        {
            GameObject *helpGameObject = firstGameObject;
            while(helpGameObject != nullptr)
            {
                helpGameObject->Update();
                helpGameObject = helpGameObject->nextGameObject;
            }
        }
    }
    void Render()
    {
        if(firstGameObject != nullptr)
        {
            GameObject *helpGameObject = firstGameObject;
            while(helpGameObject != nullptr)
            {
                helpGameObject->Render();
                helpGameObject = helpGameObject->nextGameObject;
            }
        }
    }

    void Add(GameObject *newGameObject)
    {
        if(firstGameObject == nullptr)
        {
            firstGameObject = newGameObject;
        }
        else
        {
            GameObject *helpGameObject = firstGameObject;
            while(helpGameObject != nullptr)
            {
                helpGameObject = helpGameObject->nextGameObject;
            }
            helpGameObject = newGameObject;
        }
    }
};

int main()
{
    GameObjectManager gom;
    bool run = true;

    gom.Add(new GameObject);
    gom.Add(new GameObject);
    gom.Add(new GameObject);
    gom.Add(new GameObject);

    while(run)
    {
        cout<<"GameLoop Start\n";
        gom.Update();
        gom.Render();
        cout<<"GameLoop End\n";
        cin.get();
    }

    return 0;
}
#包括
使用名称空间std;
类游戏对象
{
公众:
GameObject*nextGameObject;
游戏对象()
{

cout对于链表来说,这是一个可怕的解决方案,但我会回答你的问题。错误在于Add()。在这里,你只需修改局部变量helpGameObject。相反,你应该停在没有后继对象的对象上,然后修改下一个对象。

问题在于你的
Add
函数

下面一行:

helpGameObject = newGameObject;
实际上不会更改由
helpGameObject
指向的值,而是更改指针本身

我认为最好的解决办法是将其改为以下内容

        GameObject *helpGameObject = firstGameObject;
        while(helpGameObject->nextGameObject != nullptr)
        {
            helpGameObject = helpGameObject->nextGameObject;
        }
        helpGameObject->nextGameObject = newGameObject;

您可能感兴趣。请每个问题问一个问题,第四个问题:为什么不使用更简单的
GameObject
类解决
Manager
中的bug?