Вызвать метод абстрактного класса внутри связанного списка не удается

Я написал класс с именем 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;
};

Внутри ino проекта arduino я запускаю 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);
}
}

Но в этом случае ничего не происходит. Обновление Синуса не вызывается.
И последнее, но не менее важное, вот мой двойной связанный список. (Я проверил это, но, возможно, есть какая-то проблема с этим?)

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

0

Решение

Ошибка действительно очень мала, и я заметил это просто по счастливой случайности.

Я изменил счетчик списка на байтовый, чтобы уменьшить количество шагов. Но если мой список пуст, это -1, так что это не будет работать. Это все! изменил счетчик на короткий тип, и это работает!

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

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

Другие решения


По вопросам рекламы ammmcru@yandex.ru
Adblock
detector