繁体   English   中英

未定义的对'vtable for DigitalClock'的引用 - 对'DigitalClock :: staticMetaObject'的未定义引用 - Qt

[英]undefined reference to `vtable for DigitalClock' - undefined reference to `DigitalClock::staticMetaObject' - Qt

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

我的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

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

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

在单独的头文件中并将其包含在cpp中。 别忘了把头文件名这样放在项目文件中

HEADERS + = \\ digitalclock.h

Q_OBJECT不能在一个文件中工作。 希望能帮助到你。

此错误主要是因为您可能提供了错误的引用。

  1. 检查是否已在程序中添加了所有标题
  2. 检查pri文件
  3. 检查是否尝试为具有单例对象的类创建对象

即,如果您已创建单个对象,则不允许创建对象。 例:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM