0

我写了一个名为 cube 的类,它包含一个双链表,它应该包含一个抽象类的对象,Animation并有一个添加动画的方法。它看起来像他的:

class CubeLib
{
protected:
//some more variables 
    LinkedList<Animation*> animations; //a list of animations
public:
    inline void addAnimation(Animation* a){
        animations.add(a);
    };
}

界面:

class Animation
{
 public:
    virtual void update(short delta) = 0; 
};

在 arduino 项目的 ino 内部,我在全局和设置内部初始化 CubeLib 和动画,我将它们添加到列表中:

CubeLib cube;
Sinus* sinus_ani =new Sinus(&cube); // if not like this it stucks at setup?!
void setup()
{
    cube.addAnimation(sinus_ani);
}

在一个名为 render 的方法中,我调用了当前的动画更新函数。

inline void CubeLib::update(short delta)
{
    if (animations.size() != -1){ //if its not empty
        animations[current_Animation]->update(delta);
    }
}

但在这种情况下,它确实什么也没发生。不会调用 Sinus 的更新。最后但并非最不重要的是我的双链表。(我测试了它,但也许它有一些问题?)

template <typename T>
class LinkedList
{
protected:
private:
    struct Node
    {
        Node* prev;
        Node* next;
        T value;
    };

    Node* last;
    Node* first;
    byte count;

public:
    LinkedList()
    {
        count = -1; //empty
    };

    ~LinkedList()
    {
        if (count > -1){
            clear();
        }
    };
    /** adds to list*/
    inline void add(T t);

    /**removes the thing at index*/
    inline T remove(int index);

    /** Returns NULL(ptr) if index is out of range or item not found somehow*/
    inline T get(int index);

    inline void clear();

    /**Returns the first obj*/
    inline T getFirst();

    /**Returns the last obj*/
    inline T getLast();

    /**Returns the current size. If -1 its empty!*/
    inline int size(){
        return count;
    };

    T operator[](const int i)
    {
        return get(i);
    };
};

template <typename T>
inline void LinkedList<T>::add(T t){
    Node* n = new Node();
    n->value = t;
    if (count > -1)
    {
        n->next = first;
        n->prev = last;
        last->next = n;
        last = n;
        count++;
    }
    else if (count == -1)//first element
    {
        first = n;
        first->next = n;
        first->prev = n;
        last = n;
        last->next = n;
        last->prev = n;
        count++;
    }
}

template <typename T>
inline T LinkedList<T>::remove(int index){
    if (index <= count)
    {
        Node* n = last;
        for (int i = 0; i <= index; i++)
        {
            n = n->next;
        }
        n->prev->next = n->next;
        n->next->prev = n->prev;
        count--;
        return n->value; //return the value of that node
    }
}

template <typename T>
inline T LinkedList<T>::get(int index){
    if (index <= count && index > -1)
    {
        Node* n = first;
        int i = 0;
        while (i < index)
        {
            n = n->next;
            i++;
        }
        return n->value;
    }
    return NULL;
}

template <typename T>
inline void LinkedList<T>::clear()
{
    Node* n = first;
    while (count > 0)
    {
        Node* toBeDeleted = n;
        n = n->next;
        delete toBeDeleted;
        count--;
    }
}
/**Returns the first obj*/
template <typename T>
inline T LinkedList<T>::getFirst()
{
    return first->value;
};

/**Returns the last obj*/
template <typename T>
inline T LinkedList<T>::getLast()
{
    return last->value;
};

我很抱歉这里有很多代码。我希望它不是一个明显的失败者。


编辑:

窦被宣布喜欢他的:

class Sinus : public Animation
{
private:
    RGB color;
    CubeLib* cube;
    byte colorcounter;
    float time;

public:
    Sinus(CubeLib* c) : time(0.0), colorcounter(0), cube(c){
        color.r = MAX_COLOR;
        color.g = MAX_COLOR;
        color.b = MAX_COLOR;
    };
    ~Sinus(){};
    void update(short delta);
};

void Sinus::update(short delta)
{
    //do this 1000 times
    time += (((float)delta)/1000.0);
    for (int x = 0; x < 5; x++)
    {
        float value = (2.0*sin((float)(x + 1)*time*12.0)) + 2.0;
        for (int y = 0; y < 5; y++)
        {
            for (int z = 0; z < 5; z++)
            {
                if (abs(((float)z) - value) < 0.5)
                {
                    //cube.setLED(x, y, z, depth - 30/5*(int)abs(z-value), 0, 0);
                    cube->setLED(x, y, z, color);
                }
                else
                {
                    cube->setLED(x, y, z, 0, 0, 0);
                }
            }
        }
    }

    colorcounter++;
    if (colorcounter > 25)
    {
        color.r = random(MAX_COLOR);
        color.g = random(MAX_COLOR);
        color.b = random(MAX_COLOR);
        colorcounter = 0;
    }
}
4

1 回答 1

0

错误真的非常非常小,我只是幸运地注意到了它。

我将列表的计数器更改为字节以减少步骤。但是如果我的列表是空的,它是-1,所以这不起作用。就这样!将计数更改为短类型,它可以工作!

template <typename T>
class LinkedList
{
private:
    struct Node
    {
        Node* prev;
        Node* next;
        T value;
    };

    Node* last;
    Node* first;
    short count; // HERE 
/...
};
于 2014-11-28T16:04:10.383 回答