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 MyClass'
QtWS25 Last Chance

Error: undefined reference to `vtable for MyClass'

Scheduled Pinned Locked Moved Solved General and Desktop
14 Posts 8 Posters 85.2k 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.
  • ? This user is from outside of this forum
    ? This user is from outside of this forum
    Guest
    wrote on last edited by A Former User
    #1

    What am I doing wrong?

    @class MyClass : public QObject{
    Q_OBJECT
    public:
    MyClass(const QString& text, QObject* parent = 0)
    :m_text(text), QObject(parent){

        }
    
        const QString& text() const{return m_text;}
        int getLengthOfText() const{return m_text.length();}
    

    public slots:
    void setText(const QString& text){m_text = text;}

    signals:
    void textChanged(const QString&);

    private:
    QString m_text;

    };@

    I get the error on the line of the constructor initializer list, line 5

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

      Did you run moc on it?

      1 Reply Last reply
      1
      • ? This user is from outside of this forum
        ? This user is from outside of this forum
        Guest
        wrote on last edited by
        #3

        [quote author="Andre" date="1323249955"]Did you run moc on it?
        [/quote]

        Doesn't it run automatically on building the project with Qt Creator? If not, what steps do I need to take to run it manually?

        1 Reply Last reply
        0
        • Q Offline
          Q Offline
          qxoz
          wrote on last edited by
          #4

          What is vtable?
          Or maybe you didn't post all code of your class.

          I create new class and paste yours class as is and there are no errors.

          1 Reply Last reply
          0
          • sierdzioS Offline
            sierdzioS Offline
            sierdzio
            Moderators
            wrote on last edited by
            #5

            Right click on your project and select "Run qmake" to for a new build of MOC classes. It usually does run automatically, but sometimes it "forgets to".

            @qxoz - vtable is virtual functions' reference table. "LINK":http://en.wikipedia.org/wiki/Virtual_method_table

            (Z(:^

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

              [quote author="ddriver" date="1323250137"]
              [quote author="Andre" date="1323249955"]Did you run moc on it?
              [/quote]

              Doesn't it run automatically on building the project with Qt Creator? If not, what steps do I need to take to run it manually?
              [/quote]
              Well, you did not show us the .pro file or anything like that, so there was no way for me to know what you use to develop this thing. I would first try the Build > Run qmake command from the menu. If that doesn't work, do a complete rebuild.

              BTW: you realize that your textChanged() signal will not be emitted by your current code?

              1 Reply Last reply
              0
              • ? This user is from outside of this forum
                ? This user is from outside of this forum
                Guest
                wrote on last edited by
                #7

                I clean the project, run qmake manually, rebuild and still get the same error.

                Andre - I had the emit in the implementation but removed it to test with simpler inlined definitions.

                The problem probably lies in the .pro file - I haven't done anything special to it, just started a new console application. Here is the content Qt Creator generated for me:
                @QT += core

                QT -= gui

                TARGET = untitled
                CONFIG += console
                CONFIG -= app_bundle

                TEMPLATE = app

                SOURCES += main.cpp@

                Any suggestions?

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

                  Yes: add the actual header and source files using the HEADERS and SOURCES variables.

                  Currently, qmake has no idea about your class and that it needs to build it. Note that HEADERS is strictly speaking only needed for objects that need the meta-object framework to do its magic on them (that includes all QObject subclasses), but it is a good habbit to just include all headers there; at least in a pure Qt application.

                  Jing XuJ 1 Reply Last reply
                  1
                  • ? This user is from outside of this forum
                    ? This user is from outside of this forum
                    Guest
                    wrote on last edited by
                    #9

                    Thanks a lot, I will keep this limitation in mind from now on, I have implemented many classes in the main.cpp before and had no issues, but that was probably because I was doing very basic stuff with standard c++.

                    The error is gone after I put the class into a separate header file.

                    1 Reply Last reply
                    1
                    • sierdzioS sierdzio

                      Right click on your project and select "Run qmake" to for a new build of MOC classes. It usually does run automatically, but sometimes it "forgets to".

                      @qxoz - vtable is virtual functions' reference table. "LINK":http://en.wikipedia.org/wiki/Virtual_method_table

                      I Offline
                      I Offline
                      Ivanov228
                      Banned
                      wrote on last edited by
                      #10
                      This post is deleted!
                      1 Reply Last reply
                      0
                      • E Offline
                        E Offline
                        Edward_Abrosimov
                        wrote on last edited by
                        #11

                        And if you use cmake, add the following lines to the CMakeList.txt file:

                        SET(CMAKE_AUTOMOC ON)
                        SET(CMAKE_AUTOUIC ON)
                        SET(CMAKE_AUTORCC ON)

                        1 Reply Last reply
                        0
                        • A andre

                          Yes: add the actual header and source files using the HEADERS and SOURCES variables.

                          Currently, qmake has no idea about your class and that it needs to build it. Note that HEADERS is strictly speaking only needed for objects that need the meta-object framework to do its magic on them (that includes all QObject subclasses), but it is a good habbit to just include all headers there; at least in a pure Qt application.

                          Jing XuJ Offline
                          Jing XuJ Offline
                          Jing Xu
                          wrote on last edited by
                          #12

                          @andre

                          Thanks. It's helpful.

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

                            I just removed the files from project (having Q_OBJECT) and added again, it could compile and build. I am not sure about the error...

                            aha_1980A 1 Reply Last reply
                            0
                            • A Asheesh

                              I just removed the files from project (having Q_OBJECT) and added again, it could compile and build. I am not sure about the error...

                              aha_1980A Offline
                              aha_1980A Offline
                              aha_1980
                              Lifetime Qt Champion
                              wrote on last edited by
                              #14

                              Hi @asheesh,

                              the correct way is to run qmake and then (re)build the project. With removing and adding the files you probably triggered this.

                              QtCreator 4.10 contains a fix to re-run qmake on project rebuild, so this error occur much less nowadays.

                              Regards

                              Qt has to stay free or it will die.

                              1 Reply Last reply
                              1

                              • Login

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