I've been trying to create a timer that every time it runs out it will add something to a list. To do this i need to add a slot to the signal of the timer running out.
#ifndef GAMEMODEL_H
#define GAMEMODEL_H
#include <QObject>
#include <QTimer>
class GameModel: public QObject{
Q_OBJECT
private:
listaLinkata<Energy> *energySources;
QTimer* energyTimer;
public:
explicit GameModel(QObject *parent = nullptr);
~GameModel() = default;
void addEnergy(const Energy& energyItem);
private slots:
void generateEnergy();
};
#endif // GAMEMODEL_H
.cpp
#include "gamemodel.h"
#include <QTimer>
GameModel::GameModel(QObject *parent) : QObject(parent)
{
energyTimer = new QTimer(this);
energyTimer->setInterval(30000);
QObject::connect(energyTimer, &QTimer::timeout, this, &GameModel::generateEnergy);
energyTimer->start();
}
//this is the slot and assume the functions inside are working.
void GameModel::generateEnergy() {
Energy newEnergy("NewEnergy");
addEnergy(newEnergy);
}
}
and this is the error tham I'm facing.
:-1: error: debug/main.o:main.cpp:(.rdata$.refptr._ZTV9GameModel[.refptr._ZTV9GameModel]+0x0): undefined reference to `vtable for GameModel'
I can't make the slot work, no matter the changes I do. I'm already using a slot and signal on my main and even on a View and they work, why not here? Btw, I'm using an MVC pattern not that matters for this particualr problem.
I tried with chatGPT but that thing is telling me to create dummy functions to override something from QObject but in the end nothing worked. After that it told me to check if I've iherithed from QObject correctly and I think I did.