2012-02-14 62 views
3
[email protected]:~/Desktop/notes/pomodoro> ls 
timer.cpp 

[email protected]:~/Desktop/notes/pomodoro> qmake -project 
[email protected]:~/Desktop/notes/pomodoro> qmake 
[email protected]:~/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 

我pomodoro.pro:未定義的參考`虛表的DigitalClock ' - 未定義參考`DigitalClock :: staticMetaObject' - Qt的

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

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

# Input 
SOURCES += timer.cpp 

我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(); 
} 

回答

7

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

在單獨的頭文件和包括我t到cpp。 不要忘記把頭文件名的項目文件中這樣

HEADERS + = \ digitalclock.h

Q_OBJECT是不會在一個文件中工作。 希望它有幫助。

+0

可能會有幫助,我會嘗試,但是這是行不通的原因是什麼? – 2012-02-14 05:17:39

+0

只是爲了原因如何編寫宏。 – 2012-02-14 05:21:26

+1

請參閱http://stackoverflow.com/questions/1368584/qt-question-what-does-the-q-object-macro-do-why-do-all-qt-objects-need-this-ma它解釋了什麼Q_OBJECT does – dirk 2012-02-14 05:40:11

3

此錯誤主要是因爲您可能給出了錯誤的引用。

  1. 檢查您是否已經添加的所有頭需要在程序
  2. 檢查PRI文件你
  3. 檢查是否想爲班級創建對象有singleton對象

即如果你創建了一個單例對象,它將不允許創建對象。例如:

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