Friday, 14 July 2017

c++ - LNK2019 unresolved External Symbol for a QObject



I'm getting the following errors when building with QMake:






  • LNK2019: unresolved external symbol "public: void __cdecl TimerTodo::notify(class TodoBaseTask *)" (?notify@TimerTodo@@QEAAXPEAVTodoBaseTask@@@Z) referenced in function "private: void __cdecl TimerTodo::timerOver(void)" (?timerOver@TimerTodo@@AEAAXXZ)


  • LNK2019: unresolved external symbol "public: void __cdecl TimerTodo::hasNotified(class TimerTodo *)" (?hasNotified@TimerTodo@@QEAAXPEAV1@@Z) referenced in function "private: void __cdecl TimerTodo::timerOver(void)" (?timerOver@TimerTodo@@AEAAXXZ)


  • LNK1120: 2 unresolved externals





This is my header:



#ifndef TIMERTODO_H
#define TIMERTODO_H


#include

class TodoBaseTask;

class TimerTodo : public QTimer
{
public:
TimerTodo(TodoBaseTask *timer);
void StartTimer();

private slots:
void timerOver();
signals:
void notify(TodoBaseTask *todo);
void hasNotified(TimerTodo *timer);
private:
TodoBaseTask *m_todo;
};

#endif // TIMERTODO_H



And this is my source:



#include "timertodo.h"
#include "todobasetask.h"

TimerTodo::TimerTodo(TodoBaseTask *todo)
{
m_todo = todo;

connect(this, SIGNAL(timeout()), this, SLOT(timerOver()));
}

void TimerTodo::StartTimer()
{
QDateTime nextNotify = m_todo->getDeadLine().addDays(-1);
this->start(QDateTime::currentDateTime().msecsTo(nextNotify));
}

void TimerTodo::timerOver()

{
emit notify(m_todo);
emit hasNotified(this);
}


How to fix it?


Answer



This is explained in Qt documentation:





The moc tool reads a C++ header file. If it finds one or more class declarations that contain the Q_OBJECT macro, it produces a C++ source file containing the meta-object code for those classes. Among other things, meta-object code is required for the signals and slots mechanism, the run-time type information, and the dynamic property system.




(emphasis mine)



So you need to put this macro in every class that has its own signals or slots.


No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...