Undefined reference to `vtable for TestTimer'
-
Hi all,
I try a QTimer in a console application with qtcreator
and it does not compile ! I get this message "undefined reference to `vtable for TestTimer'"
Here is my code:@#include <QCoreApplication>
#include <QTimer>
#include <Qdebug> // pourquoi ??#include <stdio.h> // Pour printf
//**************************************************
class TestTimer : public QObject
{
Q_OBJECT //Macro qui sert à ddeclarer et connecter les signals et slots
// à mettre toujours meme si c'est superflupublic: TestTimer(void) ; ~TestTimer(void); public: int Seconde; QTimer *Timer; public slots: void EveilTimer(void);
};
void TestTimer::EveilTimer(void)
{
Seconde++;
printf("Seconde=%d\n",Seconde);
}TestTimer::TestTimer(void)
{
Timer = new QTimer(this);
connect (Timer,SIGNAL( timeout() ),this, SLOT( TestTimer::EveilTimer() ) );
Timer->start(1000);
}TestTimer::~TestTimer(void)
{
Timer->stop();
delete Timer ;
}
//****************************************************
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);TestTimer MonTimer; return a.exec();
}
and my project
QT += core
QT -= gui
TARGET = TestTimer
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cppThanks for your help
Montin
-
Here is the code with the good syntax. I'm sorry
.@#include <QCoreApplication>
#include <QTimer>
#include <Qdebug> // pourquoi ??#include <stdio.h> // Pour printf
//**************************************************
class TestTimer : public QObject
{
Q_OBJECT //Macro qui sert à declarer et connecter les signals et slots
// à mettre toujours meme si c'est superflupublic: TestTimer(void) ; ~TestTimer(void); public: int Seconde; QTimer *Timer; public slots: void EveilTimer(void);
};
void TestTimer::EveilTimer(void)
{
Seconde++;
printf("Seconde=%d\n",Seconde);
}TestTimer::TestTimer(void)
{
Seconde = 0;
Timer = new QTimer(this);
connect (Timer,SIGNAL( timeout() ),this, SLOT( TestTimer::EveilTimer() ) );
Timer->start(1000);
}TestTimer::~TestTimer(void)
{
Timer->stop();
delete Timer ;
}
//****************************************************
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);TestTimer MonTimer; return a.exec();
}
@ -
You probably forgot to include the header in your main file:
@
#include "testtimer.h"
@Also, QObjects require the implementation to be in a separate file .cpp file (well, sort of. If you want to avoid problems, better stick to it :)).
-
Hi,
To add to sierdzio, if you add/remove Q_OBJECT you need to run qmake again before building
-
It works if i have three files
-TestTimer.h with the definition of the class
_TestTimer.cpp with the code of the function in the class- main.cpp to invoqke the functions
But i did not change anything else.
Who can explain ??
Thanks -
Here is the code with the good syntax. I'm sorry
.@#include <QCoreApplication>
#include <QTimer>
#include <Qdebug> // pourquoi ??#include <stdio.h> // Pour printf
//**************************************************
class TestTimer : public QObject
{
Q_OBJECT //Macro qui sert à declarer et connecter les signals et slots
// à mettre toujours meme si c'est superflupublic: TestTimer(void) ; ~TestTimer(void); public: int Seconde; QTimer *Timer; public slots: void EveilTimer(void);
};
void TestTimer::EveilTimer(void)
{
Seconde++;
printf("Seconde=%d\n",Seconde);
}TestTimer::TestTimer(void)
{
Seconde = 0;
Timer = new QTimer(this);
connect (Timer,SIGNAL( timeout() ),this, SLOT( TestTimer::EveilTimer() ) );
Timer->start(1000);
}TestTimer::~TestTimer(void)
{
Timer->stop();
delete Timer ;
}
//****************************************************
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);TestTimer MonTimer; return a.exec();
}
@ -
Here is the code with the good syntax. I'm sorry
.@#include <QCoreApplication>
#include <QTimer>
#include <Qdebug> // pourquoi ??#include <stdio.h> // Pour printf
//**************************************************
class TestTimer : public QObject
{
Q_OBJECT //Macro qui sert à declarer et connecter les signals et slots
// à mettre toujours meme si c'est superflupublic: TestTimer(void) ; ~TestTimer(void); public: int Seconde; QTimer *Timer; public slots: void EveilTimer(void);
};
void TestTimer::EveilTimer(void)
{
Seconde++;
printf("Seconde=%d\n",Seconde);
}TestTimer::TestTimer(void)
{
Seconde = 0;
Timer = new QTimer(this);
connect (Timer,SIGNAL( timeout() ),this, SLOT( TestTimer::EveilTimer() ) );
Timer->start(1000);
}TestTimer::~TestTimer(void)
{
Timer->stop();
delete Timer ;
}
//****************************************************
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);TestTimer MonTimer; return a.exec();
}
@ -
OK and thanks a lot. I did not knew that QOject required to be in a separated file and that running qmake first is better.
My problem seems to be solved.
-
Building of Qt applications includes not only invoking the compiler, but also several code generation tools (moc, uic, rcc). Those expect your code to follow certain conventions.
-
Thanks you two.
I will not forget ! I have copied and stuck your answers as a comment in my code !! Now I am sure I will find others mistakes to do !! -
Better avoid them, you'll have more time to code :)
Happy coding !