PDA

View Full Version : Undifined Refrence: vtable(the example of C++ GUI Programming with Qt4, sec edition)



stereoMatching
19th January 2011, 11:33
I have searched for another similar threads about "undefined Reference: vtable" but still can't figured out how to solve this

my OS is windows xp sp3
my compiler is gcc4.5 mingw
my IDE is code::blokcs 10.05
my Qt version is 4.7.1

below is the code come from the book--C++ GUI Programming with Qt4, sec edition


#ifndef FINDDIALOG_H
#define FINDDIALOG_H

#include <QDialog>

class QCheckBox;
class QLabel;
class QLineEdit;
class QPushButton;

class FindDialog : public QDialog
{
Q_OBJECT

public:
FindDialog(QWidget *parent = 0);

signals:
void findNext(const QString &str, Qt::CaseSensitivity cs);
void findPrevious(const QString &str, Qt::CaseSensitivity cs);

private slots:
void findClicked();
void enableFindButton(const QString &text);

private:
QLabel *label;
QLineEdit *lineEdit;
QCheckBox *caseCheckBox;
QCheckBox *backwardCheckBox;
QPushButton *findButton;
QPushButton *closeButton;
};

#endif




#include <QtGui>

#include "finddialog.h"

FindDialog::FindDialog(QWidget *parent)
: QDialog(parent)
{
label = new QLabel(tr("Find &what:"));
lineEdit = new QLineEdit;
label->setBuddy(lineEdit);

caseCheckBox = new QCheckBox(tr("Match &case"));
backwardCheckBox = new QCheckBox(tr("Search &backward"));

findButton = new QPushButton(tr("&Find"));
findButton->setDefault(true);
findButton->setEnabled(false);

closeButton = new QPushButton(tr("Close"));

connect(lineEdit, SIGNAL(textChanged(const QString &)),
this, SLOT(enableFindButton(const QString &)));
connect(findButton, SIGNAL(clicked()),
this, SLOT(findClicked()));
connect(closeButton, SIGNAL(clicked()),
this, SLOT(close()));

QHBoxLayout *topLeftLayout = new QHBoxLayout;
topLeftLayout->addWidget(label);
topLeftLayout->addWidget(lineEdit);

QVBoxLayout *leftLayout = new QVBoxLayout;
leftLayout->addLayout(topLeftLayout);
leftLayout->addWidget(caseCheckBox);
leftLayout->addWidget(backwardCheckBox);

QVBoxLayout *rightLayout = new QVBoxLayout;
rightLayout->addWidget(findButton);
rightLayout->addWidget(closeButton);
rightLayout->addStretch();

QHBoxLayout *mainLayout = new QHBoxLayout;
mainLayout->addLayout(leftLayout);
mainLayout->addLayout(rightLayout);
setLayout(mainLayout);

setWindowTitle(tr("Find"));
setFixedHeight(sizeHint().height());
}

void FindDialog::findClicked()
{
QString text = lineEdit->text();
Qt::CaseSensitivity cs =
caseCheckBox->isChecked() ? Qt::CaseSensitive
: Qt::CaseInsensitive;
if (backwardCheckBox->isChecked()) {
emit findPrevious(text, cs);
} else {
emit findNext(text, cs);
}
}

void FindDialog::enableFindButton(const QString &text)
{
findButton->setEnabled(!text.isEmpty());
}


when I press the F9 button, the compiler always give me error message like


undefined reference to 'vtable for findDialog'
undefined reference to `FindDialog::staticMetaObject'


The example of chapter1 of this book could be compiled perfectly, but the example of
the chapter2 can't work at all.
Thanks a lot

nish
19th January 2011, 12:07
your IDE is not running qmake and/or not linking to the files generated by moc.

try to compile the example on command line via
qmake
make (or mingw-make )

stereoMatching
19th January 2011, 14:16
Thanks for your reply
I open the MinGW Command Prompt, and type the command


qmake -project
qmake
make


below is the error messages


mingw32-make[1]:***[debug\find.exe] Error 1
mingw32-make : *** [debug] Error 2


what mistakes do I make?Thanks

ps : I download my gcc from this site
htp://tdm-gcc.tdragon.net/

Added after 24 minutes:

I solved the probelm


<QT4 directory>\bin\moc.exe myfile.h -o moc_myfile.cpp
Then include the file moc_myfile.cpp in the project as well.


How could I make the IDE do this automatically for me?
Thanks

Zlatomir
19th January 2011, 14:30
Maybe you should use Qt Creator (is an very good IDE, and it "looks" exactly the same on all platforms)
It comes together with Qt SDK for Windows.

Or use Visual Studio or Eclipse, Qt framework has add-ins that integrate the Qt tools into those.

wysota
19th January 2011, 22:18
The header file containing Q_OBJECT macro has to be present in the HEADERS variable of a qmake project and qmake needs to be ran after adding the macro to the file.