c++ - 对 `vtable for DigitalClock' 的 undefined reference - 对 `DigitalClock::staticMetaObject' 的 undefined reference - Qt

标签 c++ qt compiler-errors virtual

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

我的番茄钟:

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

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

# Input
SOURCES += timer.cpp

我的定时器.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 中。 不要忘记像这样将头文件名放在项目文件中

标题 +=\ 数字时钟.h

Q_OBJECT 在一个文件中不起作用。 希望对您有所帮助。

关于c++ - 对 `vtable for DigitalClock' 的 undefined reference - 对 `DigitalClock::staticMetaObject' 的 undefined reference - Qt,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9272047/

相关文章:

c++ - 在 cpp 中使用 pthread_mutex_t

c++ - 服务没有及时响应启动或控制请求

qt - 如何获得给定文件扩展名的 Qt 图标 (QIcon)

c++ - 将 bool 转换为 QString

java - 如何用java将英语翻译成PigLatin(使用 boolean 值和字符串)

c++ - 数组大小成员函数编译错误

c++ - 作为静态类成员的对象 vector

c++ - 未找到模板重载

html - QDomDocument 按类获取元素

java - 递归的 StackOverflowError