简体   繁体   English

我怎样才能让这个链表正常工作?

[英]How can i get this Linked List to work properly?

I'm very new to linked lists and i can't seem to figure out why this isn't working.我对链表陌生,我似乎无法弄清楚为什么这不起作用。

The program doesn't crash and the compiler shows no errors but doActions() never runs.程序不会崩溃,编译器也没有显示错误,但doActions()永远不会运行。

This is the code for the function, it's called in the main loop.这是函数的代码,它在主循环中被调用。

void Action()
{
    clsParent* pCurrent;
    pCurrent = pHead;
    while(pCurrent != NULL)
    {
        clsPlayer* pPlayer;
        pPlayer = dynamic_cast<clsPlayer*>(pCurrent);
        if(pPlayer != NULL)
        {
            pPlayer->doActions();
        }
        pCurrent = pCurrent->pNext;
    }
}

This is supposed to call doActions() for every single player in the list (although there's only one).这应该为列表中的每个玩家调用doActions() (尽管只有一个)。

doAction() worked perfectly fine before i tried to implement linked lists into the code so i know it isn't that.在我尝试将链表实现到代码中之前, doAction()工作得非常好,所以我知道不是这样。 For those curious about what it does, it checks if the player is jumping and moves the player accordingly.对于那些对其功能感到好奇的人,它会检查玩家是否在跳跃并相应地移动玩家。

EDIT: I've noticed that i can put other functions in and it will work编辑:我注意到我可以把其他功能放进去,它会工作

This works:这有效:

void clsPlayer::jump()
{
    if(onGround)
    {
        jumping = true;
        yPos -= gravitySpeed;
        animationState = 3;
    }
}

While this doesn't虽然这不

void clsPlayer::doActions()
{
    if(!onGround)
    {
        yPos += gravitySpeed;
    }

    if(jumping)
    {
        jumpTimeCounter++;
        yPos -= 20;
        if(jumpTimeCounter > 10)
        {
            jumping = false;
            jumpTimeCounter = 0;
        }
    }
}

pCurrent if of type clsParent or a subclass of it. pCurrent 如果是 clsParent 类型或其子类。 The dynamic_cast to type clsPlayer will always fail and return null.输入 clsPlayer 的 dynamic_cast 将始终失败并返回 null。 Maybe there is a member data and you should use something like (the cast may even not be necessary):也许有一个成员数据,你应该使用类似的东西(甚至可能不需要演员表):

clsPlayer* pPlayer;
pPlayer = dynamic_cast<clsPlayer*>(pCurrent->data);

From the code you posted I would offer you the following proposed solution:根据您发布的代码,我将为您提供以下建议的解决方案:

template<T>
class ListNode
{
public:
    T* m_pNext;
};

class Base : public ListNode<Base>
{
public:
    Base();
    virtual ~Base();
    virtual void doActions() = 0;
};

class Derived1 : public Base
{
public:
    Derived1();
    virtual ~Derived1();
    virtual void doActions();
};

class Derived2 : public Base
{
public:
    Derived2();
    virtual ~Derived2();
    virtual void doActions();
};

void action()
{
    Base* pCurrent = pHead;
    while (pCurrent != NULL)
    {
        pCurrent->doActions();
        pCurrent = pCurrent->m_pNext;
    }
}

Things to note:注意事项:

  1. ListNode is a template class, and your base class (in your example, clsParent) inherits from it. ListNode 是一个模板类,您的基类(在您的示例中为 clsParent)继承自它。
  2. The base class declares doActions as a pure virtual function, so that derived classes may define their own specific implementation.基类将 doActions 声明为纯虚函数,以便派生类可以定义自己的特定实现。
  3. As a result of 1 & 2, notice that the loop to walk through the list and call the doActions method is simplified as we now avoid the cast.作为 1 和 2 的结果,请注意遍历列表并调用 doActions 方法的循环被简化了,因为我们现在避免了强制转换。

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

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