【问题标题】:undefined reference to `vtable for DigitalClock' - undefined reference to `DigitalClock::staticMetaObject' - Qt未定义的对“用于 DigitalClock 的 vtable”的引用 - 未定义的对“DigitalClock::staticMetaObject”的引用 - Qt
【发布时间】:2012-03-05 12:50:48
【问题描述】:
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

我的番茄钟.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();
}

【问题讨论】:

    标签: c++ qt compiler-errors virtual


    【解决方案1】:

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

    在单独的头文件中并将其包含到 cpp. 不要忘记像这样将头文件名放在项目文件中

    标题 += \ digitalclock.h

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

    【讨论】:

    • 可能有帮助,我会尝试,但这不起作用的原因是什么?
    • 只是说明宏的编写方式。
    • stackoverflow.com/questions/1368584/… 它解释了 Q_OBJECT 的作用
    • 宏引入了一个虚函数成员,并且 gcc 发出 vtable 以及在类中声明的第一个虚函数(简单的规则,因此 vtable 只生成一次)。这个虚函数以及staticMetaObject 都定义在moc 生成的.cpp 文件中;你需要运行 moc 然后编译和链接它的输出。
    • 德米特里。 :) 谢谢,实际上它确实有帮助。但我认为问题出在其他地方。此解决方案是一种“解决方法”。我的意思是整个问题的出现是因为我没有写HEADERS += \ digitalclock.h,为什么?因为。我没有!当您实际上没有任何单独的标题并且不想仅仅为了它而创建它时,应该在其中写什么?
    【解决方案2】:

    这个错误主要是因为你可能给出了错误的参考。

    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;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-30
      • 2019-07-04
      • 1970-01-01
      • 1970-01-01
      • 2012-08-14
      相关资源
      最近更新 更多