views:

3822

answers:

7

I'm using Code::Blocks 8.02 and the mingw 5.1.6 compiler. I'm getting this error when I compile my Qt project:

C:\Documents and Settings\The Fuzz\Desktop\GUI\App_interface.cpp|33|undefined reference to `vtable for AddressBook'

File AddressBook.h:

 #ifndef ADDRESSBOOK_H
 #define ADDRESSBOOK_H

 #include <QWidget>

 class QLabel;
 class QLineEdit;
 class QTextEdit;

 class AddressBook : public QWidget
 {
     Q_OBJECT

 public:
     AddressBook(QWidget *parent = 0);

 private:
     QLineEdit *nameLine;
     QTextEdit *addressText;
 };

 #endif

File AddressBook.cpp:

#include <QtGui>
#include "addressbook.h"

AddressBook::AddressBook(QWidget *parent)
     : QWidget(parent)
{
    QLabel *nameLabel = new QLabel(tr("Name:"));
    nameLine = new QLineEdit;

    QLabel *addressLabel = new QLabel(tr("Address:"));
    addressText = new QTextEdit;

    QGridLayout *mainLayout = new QGridLayout;
    mainLayout->addWidget(nameLabel, 0, 0);
    mainLayout->addWidget(nameLine, 0, 1);
    mainLayout->addWidget(addressLabel, 1, 0, Qt::AlignTop);
    mainLayout->addWidget(addressText, 1, 1);

    setLayout(mainLayout);
    setWindowTitle(tr("Simple Address Book"));
}
+4  A: 

The problem is almost certainly that you are not compiling or not linking in the generated moc_AddressBook.cpp file. (It should have been generated for you -- you are running Qt's moc on your code before compiling, right?)

To answer a little more thoroughly, the Q_OBJECT macro signals Qt's moc tool to create an extra implementation file that contains the code necessary to support QObject's meta-information system. If you had any signals or slots, it would do a few things for those as well.

An alternative solution might be to remove the Q_OBJECT macro. You probably don't want to do this, but it would help the immediate problem, and it isn't strictly necessary with the code that you've presented.

Also, I would note that your line:

#include "addressbook.h"

Should probably be:

#include "AddressBook.h"

based on how you presented the filenames in the question.

Caleb Huitt - cjhuitt
+2  A: 

Assuming you are using qmake to generate your Makefile, be sure that AddressBook.h is specified in your .pro file's HEADERS's variable, e.g.

HEADERS = AddressBook.h
Jeremy Friesner
+3  A: 

In order to automatically ensure that all moc cpp files are generated, you can get qmake to automatically generate a .pro file for you instead of writing one yourself.

Run

qmake -project

in the project directory, and qmake will scan your directory for all C++ headers and source files to generate moc cpp files for.

blwy10
+1  A: 

I had the same problem but as soon as I defined my constructor in the header file instead of the .cpp the error disappeared. Also the corresponding moc file was missing in the file system and in the Makefile section"compiler_moc_header_make_all" . I ran a qmake then finally everything built with succes. I went to check the Makefile and it was there now.

yan bellavance
A: 

I come to the same problem, rebuild the project never update the Makefile, I remove the Makefile and rebuild , the the problem is gone. ps: run 'make' from command line may give you detail information than the IDE, and helpful to get the real problem.

+1  A: 

just say "run qmake" from Build menu & then "Rebuild All" again from Build menu....problem gets solved...atleast in my case it did.

Anurag Verma
A: 

Go to .pro file and make sure .h file has 'include' before it. HEADERS += include/file.h \ include/file2.h

Phil