2012-02-14 8 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 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(); 
}; 

을 별도의 헤더 파일에 넣고 i t ~ cpp. 는

매크로 Q_OBJECT

하나 개의 파일에서 작동하지 않을이

HEADERS + = \ digitalclock.h 같은 프로젝트 파일에 헤더 파일 이름을 넣어하는 것을 잊지 말아. 도움이 되길 바랍니다.

+0

도움이 될지 모르지만 시도해 볼 수는 있습니다. 이유는 무엇입니까? –

+0

매크로가 작성된 이유만으로. –

+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는 – dirk

3

이 오류는 주로 잘못된 참조를했을 수 있기 때문에 발생합니다. 당신은 모든 헤더는

  • 당신이 그 클래스의 객체를 만들려고
  • 확인 여부 PRI 파일을 확인 프로그램에 필요한 추가 여부

    1. 확인 단일 객체

    예를 가지고, 싱글 톤 객체를 생성했다면 객체를 생성 할 수 없습니다. 예 :

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