在链表中调用抽象类的方法失败

时间:2014-11-28 11:56:56

标签: c++ arduino abstract-methods

我编写了一个名为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的方法中,我调用当前的Animations更新函数。

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;
};

我很抱歉这里有很多代码。我希望它不是一个显而易见的因素。


编辑:

Sinus被宣布为他的:

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;
    }
}

1 个答案:

答案 0 :(得分:0)

这个错误真的很小,我只是靠运气注意到它。

我将列表的计数器更改为byte以减少步骤。但如果我的列表为空,则为-1,因此这不起作用。就这样!将计数更改为短类型并且有效!

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

    Node* last;
    Node* first;
    short count; // HERE 
/...
};