Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Error: undefined reference to 'vtable for XXX'

Error: undefined reference to 'vtable for XXX'

Scheduled Pinned Locked Moved General and Desktop
15 Posts 6 Posters 8.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kritika
    wrote on last edited by
    #1

    i have created a base class called Base which inherits from QDialog:
    @
    class Base : public QDialog
    {
    Q_OBJECT
    public:
    Base ();

    --
    };
    @
    And added this file to PATH: /usr/local/include/ and its source file to /usr/local/src/

    Now I am using this as a base class in my application:
    @
    class Test:public Base
    {
    Q_OBJECT

    --
    };
    @

    The application is giving error:
    undefined reference to 'vtable for A'.
    Does anyone know why i am getting this error??

    P.S. :--Here the Base class is not an abstract class.

    [edit, code tags added, koahnig]

    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      Please check out the forum rules for "code wrappings.":http://qt-project.org/wiki/ForumHelp#e3f82045ad0f480d3fb9e0ac2d58fb01 This time I have added them for you.

      This is a very general error message typically coming up when a class is not properly initialized yet. You need to check out the initialization of members in your class respectively if the sequence of their initialization is correct when doing a recursive initialization. For the last case you should have received a warning as long as the sequence is not correct within the class.

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andre
        wrote on last edited by
        #3

        One possible (and unfortunately common with Qt and qmake) cause is that qmake did not pick up the Q_OBJECT macro in your class declaration yet. If that is the case, then the Q_OBJECT macro is declaring some methods that are not implemented by moc yet.

        Did you re-run qmake on your project before trying to rebuild?

        1 Reply Last reply
        0
        • K Offline
          K Offline
          Kritika
          wrote on last edited by
          #4

          Yes...i tried that....but it is giving the same error on building d project.

          1 Reply Last reply
          0
          • A Offline
            A Offline
            andre
            wrote on last edited by
            #5

            So, show the actual error please. My crystal ball is at the repair shop...

            1 Reply Last reply
            0
            • B Offline
              B Offline
              butterface
              wrote on last edited by
              #6

              Do you have virtual methods?

              1 Reply Last reply
              0
              • mranger90M Offline
                mranger90M Offline
                mranger90
                wrote on last edited by
                #7

                Are the above definitions in an include file or a .cpp file ?

                1 Reply Last reply
                0
                • K Offline
                  K Offline
                  Kritika
                  wrote on last edited by
                  #8

                  There is no virtual method, right now.
                  I am just testing how this concept works.
                  And these definitions are include file.

                  1 Reply Last reply
                  0
                  • K Offline
                    K Offline
                    Kritika
                    wrote on last edited by
                    #9

                    base.h file
                    @#ifndef BASE_H
                    #define BASE_H

                    #include <QDialog>

                    class Base : public QDialog
                    {
                    Q_OBJECT
                    public:

                    protected:
                    explicit Base(QWidget *parent = 0, Qt::WindowFlags f = 0);

                    signals:

                    public slots:

                    };

                    #endif // BASE_H@

                    base.cpp file
                    @#include "base.h"

                    Base::Base(QWidget *parent, Qt::WindowFlags f):
                    QDialog(parent, f)
                    {
                    }@

                    Added base.h in PATH: /usr/local/include/ and base.cpp in PATH:/usr/local/src/.., since i dont want to add this base class in my application explicitely.

                    TestBase project:
                    This is the class inherits Base class:
                    test.h file
                    @#ifndef TEST_H
                    #define TEST_H

                    #include "base.h"

                    class Test : public Base
                    {
                    Q_OBJECT

                    public:
                    explicit Test(QWidget *parent = 0);
                    ~Test();

                    private:
                    };
                    #endif // TEST_H
                    @

                    test.cpp file":
                    @#include "test.h"

                    Test::Test(QWidget *parent) :
                    Base(parent, Qt::FramelessWindowHint ),
                    {
                    }

                    Test::~Test()
                    {
                    }
                    @

                    main.cpp:
                    @#include <QtGui/QApplication>
                    #include "dialog.h"

                    int main(int argc, char *argv[])
                    {
                    QApplication a(argc, argv);
                    Dialog w;
                    w.show();

                    return a.exec&#40;&#41;;
                    

                    }
                    @

                    Errors after make command:
                    test.o: In function Test::~Test()': test.cpp:(.text+0x1f): undefined reference to vtable for Base'
                    test.cpp:(.text+0x27): undefined reference to vtable for Base' test.o: In function Test::Test(QWidget*)':
                    test.cpp:(.text+0x96): undefined reference to Base::Base(QWidget*, QFlags<Qt::WindowType>)' test.cpp:(.text+0xee): undefined reference to vtable for Base'
                    test.cpp:(.text+0xf6): undefined reference to vtable for Base' moc_test.o: In function Test::qt_metacall(QMetaObject::Call, int, void**)':
                    moc_test.cpp:(.text+0x31): undefined reference to Base::qt_metacall(QMetaObject::Call, int, void**)' moc_test.o: In function Test::qt_metacast(char const*)':
                    moc_test.cpp:(.text+0x77): undefined reference to Base::qt_metacast(char const*)' moc_test.o:(.rodata+0x20): undefined reference to Base::staticMetaObject'
                    moc_test.o:(.rodata._ZTI4Test[typeinfo for Test]+0x10): undefined reference to `typeinfo for Base'
                    collect2: ld returned 1 exit status
                    make: *** [testMMIBase] Error 1

                    1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      andre
                      wrote on last edited by
                      #10

                      [quote author="Kritika" date="1391494986"]Added base.h in PATH: /usr/local/include/ and base.cpp in PATH:/usr/local/src/.., since i dont want to add this base class in my application explicitely.
                      [/quote]
                      That is interesting. What do you mean by that? Do you mean that you're not actually compiling in base.h and base.cpp into your application? If so: that won't work. If you don't want Base in your main application, you'll need to put it in a library that you link against. Otherwise, how is your application to know at run time what base actually is?

                      1 Reply Last reply
                      0
                      • JKSHJ Offline
                        JKSHJ Offline
                        JKSH
                        Moderators
                        wrote on last edited by
                        #11

                        If you don't include base.h and base.cpp in your project directory, then they will not be compiled.

                        You have two options:

                        Put base.h and base.cpp in your project directory.

                        Compile base.h and base.cpp into an an external library. Then, link that library to your project.

                        Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                        1 Reply Last reply
                        0
                        • K Offline
                          K Offline
                          Kritika
                          wrote on last edited by
                          #12

                          @JKSH- Thanks....
                          Like u said, i compiled this base class, this created four versioned libraries:
                          libbase.so
                          libbase.so.1
                          libbase.so.1.0
                          libbase.so.1.0.0
                          and then, i linked the TestBase project against it. The application did build successfully, but giving another error on run:

                          "error while loading shared libraries: libbase.so.1: cannot open shared object file: no such file or directory"

                          Can anybody explain why i am getting this error..

                          1 Reply Last reply
                          0
                          • A Offline
                            A Offline
                            andre
                            wrote on last edited by
                            #13

                            The shared library can't be found. So, you'll need to deploy it to a location where it can be found.

                            1 Reply Last reply
                            0
                            • K Offline
                              K Offline
                              Kritika
                              wrote on last edited by
                              #14

                              The problem is fixed..
                              Now, i am adding library in the application by the option Add Library on right click. This is linking library as well as including header file. And this is working well...Although i still couldnt figure out why previous thing was not working.

                              1 Reply Last reply
                              0
                              • JKSHJ Offline
                                JKSHJ Offline
                                JKSH
                                Moderators
                                wrote on last edited by
                                #15

                                [quote author="Kritika" date="1391529236"]The problem is fixed..[/quote]Great! :)

                                [quote]Although i still couldnt figure out why previous thing was not working.[/quote]Because you didn't tell your program where to find the library previously.

                                Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                                1 Reply Last reply
                                0

                                • Login

                                • Login or register to search.
                                • First post
                                  Last post
                                0
                                • Categories
                                • Recent
                                • Tags
                                • Popular
                                • Users
                                • Groups
                                • Search
                                • Get Qt Extensions
                                • Unsolved