0

I'm working on a visualization project. It should view a 3d model on a QGLViewer. I have a subclass of QGLViewer defined like this:

class GLViewer : public QGLViewer
{
    Q_OBJECT
public:
    explicit GLViewer(QWidget *parent = 0,const QGLWidget* shareWidget=0, Qt::WFlags flags=0);
protected:
    void initializeGL();
    void resizeGL(int width, int height);
    void paintGL();
signals:
public slots:
};

Implementing the c’tor like this:

GLViewer::GLViewer(QWidget *parent, const QGLWidget* shareWidget, Qt::WFlags flags):
    QGLViewer(parent,shareWidget,flags)
{
}

I’m getting linker error:

glviewer.o: In function `GLViewer::GLViewer(QWidget*, QGLWidget const*, QFlags<Qt::WindowType>)':
glviewer.cpp:(.text+0x18): undefined reference to `vtable for GLViewer'
glviewer.cpp:(.text+0x20): undefined reference to `vtable for GLViewer'

EDIT: This is content of .pro file:

QT       += core gui opengl xml

TARGET = qglviewer-test
TEMPLATE = app

LIBS += -lqglviewer-qt4 -lGLU -lGLEW

SOURCES += main.cpp\
        mainwindow.cpp \
        glviewer.cpp

HEADERS  += mainwindow.h \
        glviewer.cpp

FORMS += mainwindow.ui

sorush-r
  • 10,490
  • 17
  • 89
  • 173

3 Answers3

3

You didn't post full implementation of your GLViewer class (only the constructor), but the reason to get this error:

undefined reference to `vtable for GLViewer'

is that you didn't implement some virtual functions (I assume either initializeGL(), resizeGL(int width, int height) or paintGL()).

BЈовић
  • 62,405
  • 41
  • 173
  • 273
  • 1
    @sorush-r: Are you sure you've implemented all of them? Perhaps you forgot to put `GLViewer::` in one of the the definitions? – Mike Seymour Apr 20 '12 at 11:39
1

As VJovic said, this kind of problem is usually caused by an unimplemented virtual function. I noticed that there is no declaration/definition of GLViewer destructor. Is the destructor of QGLViewer is a virtual function? If this is the case, try to provide a destructor for GLViewer class, this may solve your problem.

Lei Mou
  • 2,562
  • 1
  • 21
  • 29
  • but the default destructor of the `GLViewer` class will be virtual, no? – BЈовић Apr 20 '12 at 10:11
  • @VJovic If the destructor of OGLViewer is declared as virtual, ~GLViewer is virtual. Otherwise not. – Lei Mou Apr 20 '12 at 10:13
  • 1
    @sorush-r I guess there is some problem with the Marco Q_OBJECT. Have a look at this post: http://stackoverflow.com/questions/4774291/q-object-throwing-undefined-reference-to-vtable-error, hope it is useful to you. – Lei Mou Apr 20 '12 at 10:19
  • @LeiMou dtor of `QGLWidget` is declared as virtual. – sorush-r Apr 20 '12 at 10:23
1

You were not running glviewer.h through moc. That's what the error is saying. In your .pro file, change

HEADERS  += mainwindow.h \
        glviewer.cpp

to

HEADERS  += mainwindow.h \
        glviewer.h               <------
Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313