undefined reference to `Config::SingleLinkedList::staticMetaObject' and `vtable for Config::SingleLinkedList'
-
class ConfigBase : public QWidget, it has slots and signals, so Q_OBJECT is set in the code.
Now, class SingleLinkedList : public ConfigBase , it also has its own slots and signals, so Q_OBJECT is also set in the code. But i come across one problem.
error: undefined reference to `Config::SingleLinkedList::staticMetaObject'
error: undefined reference to `vtable for Config::SingleLinkedList'
collect2.exe:-1: error: error: ld returned 1 exit status
I googled many, but no use.
If you want all the project(it is a little project, but is not convenient to public), leave your email !
-
Hi,
Did you add/remove the
Q_OBJECT
macro at some point ? If so you should re-run qmake or do a full rebuild. -
@Limer said in undefined reference to `Config::SingleLinkedList::staticMetaObject' and `vtable for Config::SingleLinkedList':
class ConfigBase : public QWidget, it has slots and signals, so Q_OBJECT is set in the code.
Hello, is your class defined in a header or in a cpp file?
If it's in a header file, try deleting the whole build folder (make sure there are no Makefiles left, neither in the source nor in the build folder).
If the definition is in a cpp file, then you will have to include the generated .moc file your own by apppending something like
#include "singlelinkedlist.moc"
at the end of your cpp file. -
#ifndef CONFIG_BASE_H #define CONFIG_BASE_H #include <QPushButton> #include <QSlider> #include <QGridLayout> #include <QGroupBox> #define CONFIG_NAMESPACE_START namespace Config { #define CONFIG_NAMESPACE_END } class ConfigBase : public QWidget { Q_OBJECT public: explicit ConfigBase(QWidget *parent = 0) : QWidget(parent) { groupBox1 = new QGroupBox(tr("Speed Control")); speedSlider = new QSlider(Qt::Horizontal, groupBox1); speedSlider->setMinimum(0); speedSlider->setMaximum(300); speedSlider->setValue(150); sliderLayout = new QVBoxLayout(groupBox1); sliderLayout->addWidget(speedSlider); groupBox2 = new QGroupBox(tr("Start && Pause")); startPushButton = new QPushButton(tr("Start")); pausePushButton = new QPushButton(tr("Pause")); resetPushButton = new QPushButton(tr("Reset")); startPushButton->setEnabled(true); pausePushButton->setEnabled(false); resetPushButton->setEnabled(false); pushButtonLayout = new QGridLayout(groupBox2); pushButtonLayout->addWidget(startPushButton, 0, 0, 1, 1); pushButtonLayout->addWidget(pausePushButton, 0, 1, 1, 1); pushButtonLayout->addWidget(resetPushButton, 1, 0, 1, 1); connect(startPushButton, SIGNAL(clicked(bool)), SLOT(start())); connect(pausePushButton, SIGNAL(clicked(bool)), SLOT(pause())); connect(resetPushButton, SIGNAL(clicked(bool)), SLOT(reset())); } ~ConfigBase() {} public slots: void start() { startPushButton->setEnabled(false); pausePushButton->setEnabled(true); emit comboBoxEnabled(false); } void pause() { if (pausePushButton->text() == QString(QObject::tr("Pause"))) { pausePushButton->setText(QObject::tr("Continue")); resetPushButton->setEnabled(true); emit comboBoxEnabled(true); } else { pausePushButton->setText(QObject::tr("Pause")); resetPushButton->setEnabled(false); emit comboBoxEnabled(false); } } void reset() { pausePushButton->setText(QObject::tr("Pause")); startPushButton->setEnabled(true); pausePushButton->setEnabled(false); resetPushButton->setEnabled(false); speedSlider->setValue(150); emit comboBoxEnabled(true); } signals: void comboBoxEnabled(bool); public: QPushButton *startPushButton; QPushButton *pausePushButton; QPushButton *resetPushButton; QSlider *speedSlider; protected: QGroupBox *groupBox1; QGroupBox *groupBox2; private: QVBoxLayout *sliderLayout; QGridLayout *pushButtonLayout; }; #endif // CONFIG_BASE_H
#ifndef CONFIG_SINGLE_LINKED_LIST #define CONFIG_SINGLE_LINKED_LIST #include <QCheckBox> #include <QSpinBox> #include <QTabWidget> #include <QTextEdit> #include "config_base.h" CONFIG_NAMESPACE_START class SingleLinkedList : public ConfigBase { Q_OBJECT public: explicit SingleLinkedList(ConfigBase *parent = 0) : ConfigBase(parent) { delete groupBox1; startPushButton->setEnabled(false); insertWidget = new QWidget; insertPosGroupBox = new QGroupBox(tr("Position")); insertKeyGroupBox = new QGroupBox(tr("Key")); headCheckBox1 = new QCheckBox(tr("head")); tailCheckBox1 = new QCheckBox(tr("tail")); keyTextEdit1 = new QTextEdit; customSpinBox1 = new QSpinBox; keyTextEdit1->setPlaceholderText(tr("key text edit")); customSpinBox1->setRange(2, 10); QHBoxLayout *hBoxLayout1 = new QHBoxLayout(insertPosGroupBox); hBoxLayout1->addWidget(headCheckBox1); hBoxLayout1->addWidget(tailCheckBox1); hBoxLayout1->addWidget(customSpinBox1); QHBoxLayout *hBoxLayout2 = new QHBoxLayout(insertKeyGroupBox); hBoxLayout2->addWidget(keyTextEdit1); QVBoxLayout *vBoxLayout1 = new QVBoxLayout(insertWidget); vBoxLayout1->addWidget(insertPosGroupBox); vBoxLayout1->addWidget(insertKeyGroupBox); vBoxLayout1->setSpacing(20); findWidget = new QWidget; findKeyGroupBox = new QGroupBox(tr("Key")); keyTextEdit2 = new QTextEdit; keyTextEdit2->setPlaceholderText(tr("key text edit")); QHBoxLayout *hBoxLayout3 = new QHBoxLayout(findKeyGroupBox); hBoxLayout3->addWidget(keyTextEdit2); QVBoxLayout *vBoxLayout2 = new QVBoxLayout(findWidget); vBoxLayout2->addWidget(findKeyGroupBox); removeWidget = new QWidget; removePosGroupBox = new QGroupBox(tr("By Position")); removekeyGroupBox = new QGroupBox(tr("or By Key")); headCheckBox3 = new QCheckBox(tr("head")); tailCheckBox3 = new QCheckBox(tr("tail")); customSpinBox3 = new QSpinBox; keyTextEdit3 = new QTextEdit; customSpinBox3->setRange(2, 10); keyTextEdit3->setPlaceholderText(tr("text edit")); QHBoxLayout *hBoxLayout4 = new QHBoxLayout(removePosGroupBox); hBoxLayout4->addWidget(headCheckBox3); hBoxLayout4->addWidget(tailCheckBox3); hBoxLayout4->addWidget(customSpinBox3); QHBoxLayout *hBoxLayout5 = new QHBoxLayout(removekeyGroupBox); hBoxLayout5->addWidget(keyTextEdit3); QVBoxLayout *vBoxLayout3 = new QVBoxLayout(removeWidget); vBoxLayout3->addWidget(removePosGroupBox); vBoxLayout3->addWidget(removekeyGroupBox); vBoxLayout3->setSpacing(20); tabWidget = new QTabWidget; tabWidget->addTab(insertWidget, tr("Insert")); tabWidget->addTab(findWidget, tr("Find")); tabWidget->addTab(removeWidget, tr("Remove")); mainVBoxLayout = new QVBoxLayout(this); mainVBoxLayout->addWidget(tabWidget); mainVBoxLayout->addWidget(groupBox2); mainVBoxLayout->setSpacing(30); connect(headCheckBox1, SIGNAL(stateChanged(int)), this, SLOT(slot1(int))); connect(tailCheckBox1, SIGNAL(stateChanged(int)), SLOT(slot2(int))); connect(headCheckBox3, SIGNAL(stateChanged(int)), SLOT(slot3(int))); connect(tailCheckBox3, SIGNAL(stateChanged(int)), SLOT(slot4(int))); connect(tabWidget, SIGNAL(currentChanged(int)), SLOT(tabChangedReset(int))); } ~SingleLinkedList() {} public slots: void slot1(int state) { if (state == Qt::Checked) { tailCheckBox1->setCheckState(Qt::Unchecked); customSpinBox1->setValue(2); tailCheckBox1->setEnabled(false); customSpinBox1->setEnabled(false); } else { tailCheckBox1->setEnabled(true); customSpinBox1->setEnabled(true); } } void slot2(int state) { if (state == Qt::Checked) { headCheckBox1->setCheckState(Qt::Unchecked); customSpinBox1->setValue(2); headCheckBox1->setEnabled(false); customSpinBox1->setEnabled(false); } else { headCheckBox1->setEnabled(true); customSpinBox1->setEnabled(true); } } void slot3(int state) { if (state == Qt::Checked) { tailCheckBox3->setCheckState(Qt::Unchecked); customSpinBox3->setValue(2); tailCheckBox3->setEnabled(false); customSpinBox3->setEnabled(false); } else { tailCheckBox3->setEnabled(true); customSpinBox3->setEnabled(true); } } void slot4(int state) { if (state == Qt::Checked) { headCheckBox3->setCheckState(Qt::Unchecked); customSpinBox3->setValue(2); headCheckBox3->setEnabled(false); customSpinBox3->setEnabled(false); } else { headCheckBox3->setEnabled(true); customSpinBox3->setEnabled(true); } } void insertWidgetReset() { headCheckBox1->setCheckState(Qt::Unchecked); tailCheckBox1->setCheckState(Qt::Unchecked); customSpinBox1->setValue(2); headCheckBox1->setEnabled(true); tailCheckBox1->setEnabled(true); customSpinBox1->setEnabled(true); keyTextEdit1->clear(); } void findWidgetReset() { keyTextEdit2->clear(); } void removeWidgetReset() { headCheckBox3->setCheckState(Qt::Unchecked); tailCheckBox3->setCheckState(Qt::Unchecked); customSpinBox3->setValue(2); headCheckBox3->setEnabled(true); tailCheckBox3->setEnabled(true); customSpinBox3->setEnabled(true); keyTextEdit3->clear(); } void tabChangedReset(int index) { startPushButton->setEnabled(false); switch (index) { case 0: insertWidgetReset(); break; case 1: findWidgetReset(); break; case 2: removeWidgetReset(); break; } } void resetAll() { //insertWidgetReset(); // jian cha yi xia findWidgetReset(); removeWidgetReset(); tabWidget->setCurrentIndex(0); reset(); startPushButton->setEnabled(false); } private: QVBoxLayout *mainVBoxLayout; QTabWidget *tabWidget; QWidget *insertWidget; QWidget *findWidget; QWidget *removeWidget; QGroupBox *insertPosGroupBox; QGroupBox *insertKeyGroupBox; QCheckBox *headCheckBox1; QCheckBox *tailCheckBox1; QSpinBox *customSpinBox1; QTextEdit *keyTextEdit1; QGroupBox *findKeyGroupBox; QTextEdit *keyTextEdit2; QGroupBox *removePosGroupBox; QGroupBox *removekeyGroupBox; QCheckBox *headCheckBox3; QCheckBox *tailCheckBox3; QSpinBox *customSpinBox3; QTextEdit *keyTextEdit3; }; CONFIG_NAMESPACE_END #endif // CONFIG_SINGLE_LINKED_LIST
-
Hi
The code is fine. Compiles here.
So if still complaining, then completely delete the build folder. -
Hi @Limer,
The problem appears to be that you have two
SingleLinkedList
classes. Although they are in two separate namespaces, for whatever reason (not sure its a bug, or a known limitation), QMake is only producing a moc file for one of them - if you rename either source file, the moc will be for the other.I'd suggest you rename one of those classes.
Cheers.
-
Hi
You have the file single_linked_list.h in two folders
moc seems them as one
NMAKE:-1: warning: U4004: too many rules for target 'debug\moc_single_linked_list.cpp'so rename one of the files and it works. you can do it directly in project explorer and all
is changed correctly.ahh @Paul-Colby beat me to it :)