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. [Solved] Undefined reference to vtable - class in main.cpp
Forum Updated to NodeBB v4.3 + New Features

[Solved] Undefined reference to vtable - class in main.cpp

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 26.5k Views 1 Watching
  • 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.
  • M Offline
    M Offline
    Mathai
    wrote on last edited by
    #1

    In the following code I have a class derived from QMainWindow in the file 'main.cpp'. I know this is not the standard way of writing. This code give an error when compiled - 'undefined reference to vtable for TopLevelWindow' . This can be avoided if I put TopLevelWindow in a headerfile. Could somebody explain why this happens?

    @
    //main.cpp
    #include <QtGui/QApplication>
    #include <QMainWindow>

    class TopLevelWindow:public QMainWindow{

    Q_OBJECT
    

    public:
    TopLevelWindow( QWidget *parent = 0){

    }
    

    };

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    TopLevelWindow tWin;
    tWin.show();
    return a.exec();
    }@

    There are posts that talk about this error, but not in this context.

    Thank you!

    1 Reply Last reply
    1
    • D Offline
      D Offline
      dbzhang800
      wrote on last edited by
      #2

      Add following line before or after you main(){}

      @
      #include "main.moc"
      @

      1 Reply Last reply
      0
      • M Offline
        M Offline
        Mathai
        wrote on last edited by
        #3

        Thank you very much. it worked! I am curious about how this works and what it does. I could not find any pointer on why it works.

        1 Reply Last reply
        0
        • G Offline
          G Offline
          goetz
          wrote on last edited by
          #4

          If you put the class into it's own header file, qmake generates the proper makefile magic to link to the separately compiled moc file. If you put it into a .cpp file, this code is not generated.

          http://www.catb.org/~esr/faqs/smart-questions.html

          1 Reply Last reply
          0
          • D Offline
            D Offline
            dbzhang800
            wrote on last edited by
            #5

            We all know that subclass of QObject with Q_OBJECT must be moced.

            @
            moc xxx.h -o moc_xxx.cpp
            @

            or

            @
            moc xxx.cpp -o xxx.moc
            @

            And we know that, generated file contains implemention of the class. So it must be compiled and linked.

            It's easy to understand, when we compile the gererated file, we should include definition of the class. But...


            Now, what's the difference between moc_xxx.cpp and xxx.moc ?

            moc_xxx.cpp is easy to be compiled. you can found #include "xxxx.h" in this file. So it is a compile unit.
            @
            cl /c moc_xxx.cpp
            @
            or
            @
            g++ -c moc_xxx.cpp
            @

            but for xxx.moc, you can not do such thing.

            • When you compile it, class definition must be found, but you can not #include"xxx.cpp" in this file.

            so

            @
            g++ -c xxx.moc
            @
            can not pass.

            It must be included in your xxx.cpp, then
            @
            g++ -c xxx.cpp
            @

            1 Reply Last reply
            0
            • M Offline
              M Offline
              Mathai
              wrote on last edited by
              #6

              Ah that explains it. Thank you guys.

              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