PDA

View Full Version : "undefined reference for vtable" problem



jepessen
14th September 2011, 19:13
Hi.

I've downloaded a simple application to see how I can draw using Opengl.

The project has only the main.cpp that have the following code:

#include <QtGui/QApplication>
#include <QGLWidget>
#include <QHBoxLayout>
#include <QSize>
#include <QPushButton>

class MyGLWidget : public QGLWidget
{
Q_OBJECT
public:
MyGLWidget (QWidget* parent ) : QGLWidget(parent)
{
}
float red, blue, green;
void initializeGL()
{
red = 0;
blue = 0;
green = 0;
}

public slots:

void resetColor()
{
red = 0;
blue = 0;
green = 0;
update();
}

void paintGL()
{
red += 0.007;
blue += 0.002;
green += 0.005;
glClearColor(red,green,blue,0);
glClear(GL_COLOR_BUFFER_BIT);
}
};


class MiaMainWindow:public QWidget
{
public:
MiaMainWindow ():QWidget(NULL){
MyGLWidget *glWidget = new MyGLWidget(this);
QHBoxLayout *layout = new QHBoxLayout(this);
QPushButton *button = new QPushButton("Get to the Choppa!!!", this);
setLayout(layout);
layout->addWidget(glWidget);
layout->addWidget(button);
connect(button, SIGNAL(clicked()),glWidget, SLOT(resetColor()));
setWindowTitle("Hello World!");
sizeHint();
}

QSize sizeHint() const
{
return QSize(100,100);
}

};

int main(int argc, char** argv){

QApplication app(argc, argv);
MiaMainWindow win;
win.show();
int res = app.exec();
return res;
}


and the .pro is the following one:


#-------------------------------------------------
#
# Project created by QtCreator 2011-09-14T20:01:51
#
#-------------------------------------------------

QT += core
QT += gui
QT += opengl

TARGET = tryme
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp


But, when I compile it (using qtcreator 2.3.0 with Qt 4.7.4 and MinGW 4.4), I've the following errror:


debug/main.o: In function `MyGLWidget':
dir\tryme-build-desktop-Qt_4_7_3_for_Desktop_-_MinGW_4_4__Qt_SDK__Debug/../tryme/main.cpp:12: undefined reference to `vtable for MyGLWidget'
dir\tryme-build-desktop-Qt_4_7_3_for_Desktop_-_MinGW_4_4__Qt_SDK__Debug/../tryme/main.cpp:12: undefined reference to `vtable for MyGLWidget'
collect2: ld returned 1 exit status
mingw32-make[1]: *** [debug\tryme.exe] Error 1
mingw32-make: *** [debug] Error 2
The process "C:\QtSDK\mingw\bin\mingw32-make.exe" exited with code 2.
Error while building project tryme (target: Desktop)
When executing build step 'Make'

I've read something about this error but I don't know where's the error. Can anyone help me, please?

Thanks in advance for your replies.

Regards.

wysota
14th September 2011, 19:23
qmake only looks for Q_OBJECT macro in header files. So either move your class definition to a header file (and add it to your project and rerun qmake) or force running moc on your main.cpp by writing #include "main.moc" under the class definition (and rerun qmake).

kalgorithmist
14th September 2011, 19:28
That's strange, but try to remove the Q_OBJECT macro from the class definition.

jepessen
14th September 2011, 19:30
Really thanks. It solved my problem :-)