3
anisha@linux-dopx:~/Desktop/notes/pomodoro> ls
timer.cpp

anisha@linux-dopx:~/Desktop/notes/pomodoro> qmake -project
anisha@linux-dopx:~/Desktop/notes/pomodoro> qmake
anisha@linux-dopx:~/Desktop/notes/pomodoro> make
g++ -c -m64 -pipe -O2 -Wall -W -D_REENTRANT -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I../../../qtsdk-2010.05/qt/mkspecs/linux-g++-64 -I. -I../../../qtsdk-2010.05/qt/include/QtCore -I../../../qtsdk-2010.05/qt/include/QtGui -I../../../qtsdk-2010.05/qt/include -I. -I. -o timer.o timer.cpp
g++ -m64 -Wl,-O1 -Wl,-rpath,/home/anisha/qtsdk-2010.05/qt/lib -o pomodoro timer.o    -L/home/anisha/qtsdk-2010.05/qt/lib -lQtGui -L/home/anisha/qtsdk-2010.05/qt/lib -L/usr/X11R6/lib64 -lQtCore -lpthread 
timer.o: In function `DigitalClock::DigitalClock(QWidget*)':
timer.cpp:(.text+0x151): undefined reference to `vtable for DigitalClock'
timer.cpp:(.text+0x159): undefined reference to `vtable for DigitalClock'
timer.cpp:(.text+0x1bc): undefined reference to `DigitalClock::staticMetaObject'
timer.o: In function `main':
timer.cpp:(.text+0x2c0): undefined reference to `vtable for DigitalClock'
timer.cpp:(.text+0x2c9): undefined reference to `vtable for DigitalClock'
timer.cpp:(.text+0x30f): undefined reference to `vtable for DigitalClock'
timer.cpp:(.text+0x318): undefined reference to `vtable for DigitalClock'
collect2: ld returned 1 exit status
make: *** [pomodoro] Error 1

My pomodoro.pro:

######################################################################
# Automatically generated by qmake (2.01a) Tue Feb 14 10:32:09 2012
######################################################################

TEMPLATE = app
TARGET = timer
DEPENDPATH += .
INCLUDEPATH += .

# Input
SOURCES += timer.cpp

My timer.cpp:

#include <QLCDNumber>
#include <QtGui>
#include <QApplication>

class DigitalClock : public QLCDNumber
{
    Q_OBJECT
    public:
        DigitalClock (QWidget *parent = 0);
    private slots:
        void showTime();
};

DigitalClock :: DigitalClock (QWidget *parent) : QLCDNumber (parent)
{
    setSegmentStyle(Filled);

    QTimer *timer = new QTimer(this);
    connect (timer, SIGNAL(timeout()), this, SLOT(showTime()));
    timer->start (1000);

    showTime();

    setWindowTitle (tr ("Digital Clock"));
    resize (150, 60);
}

void DigitalClock :: showTime()
{
    QTime time = QTime::currentTime();
    QString text = time.toString("hh:mm");
    if ((time.second() % 2) == 0)
        text[2] = ' ';
    display(text);
}

int main (int argc, char *argv[])
{
    QApplication app(argc, argv);

    DigitalClock clock;
    clock.show();

    return app.exec();
}
Aquarius_Girl
  • 21,790
  • 65
  • 230
  • 411

2 Answers2

8

Put

class DigitalClock : public QLCDNumber
{
    Q_OBJECT
    public:
        DigitalClock (QWidget *parent = 0);
    private slots:
        void showTime();
}; 

in separate header file and include it to cpp. dont forget to put header file name in project file like this

HEADERS += \ digitalclock.h

The macro Q_OBJECT is not going to work in one file. Hope it helps.

Dmitriy Kachko
  • 2,804
  • 1
  • 19
  • 21
  • May be it helps, I'll try, but what's the reason this is not working? – Aquarius_Girl Feb 14 '12 at 05:17
  • Just for reason how the macro is written. – Dmitriy Kachko Feb 14 '12 at 05:21
  • 1
    See http://stackoverflow.com/questions/1368584/qt-question-what-does-the-q-object-macro-do-why-do-all-qt-objects-need-this-ma It explains what Q_OBJECT does – Dirk Feb 14 '12 at 05:40
  • The macro introduces a virtual function member, and gcc emits the vtable along with the first virtual function declared in a class (simple rule so the vtable is generated only once). This virtual function as well as the `staticMetaObject` are defined in the *.cpp* file generated by the `moc`; you need to run moc and then compile and link its output as well. – Simon Richter Feb 14 '12 at 05:45
  • Dmitry. :) Thanks, actually it did help. But I think the problem is somewhere else. This solution is a "work around". I mean this whole problem arose because I didn't write `HEADERS += \ digitalclock.h`, why? coz. I didn't have any! What to write in there when you actually don't have any separate header and don't want to create it just for the heck of it? – Aquarius_Girl Feb 14 '12 at 14:11
  • In fact all your problems because macros thinks that you have one header for each derived from QObject class. May be you shouldn't have separate file for header for this particular case, just enough to write it in a project file, I don't know. But my friendly advise for you - use separate files unless you are not very experienced. You can use QTCreator for automatic generation of those and as a comfortable environment for education. Hope it helps and don't forget to mark my answer if it helps ;) – Dmitriy Kachko Feb 14 '12 at 15:15
3

This error is mainly because you might given a wrong reference.

  1. Check whether you have added all the headers need in the program
  2. Check the pri file
  3. Check whether you tried to create object for the class that having singleton object

i.e, If you have created a singleton object it won't allow to create object. Example:

//JSONDataManager class having singleton object
JSONDataManager* JSONDataManager::instance = 0;

JSONDataManager* JSONDataManager::Instance() {
    if (instance == 0) {
        instance = new JSONDataManager;
    }
    return instance;
}

//You can access its members as follows
JSONDataManager::Instance()->method();


//You cannot do as follows
JSONDataManager jsonManager;
user35443
  • 6,309
  • 12
  • 52
  • 75
JAI
  • 123
  • 1
  • 2
  • 11