Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved Error: undefined reference to `vtable for MyClass'

    General and Desktop
    8
    14
    80085
    Loading More Posts
    • 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.
    • ?
      Guest last edited by A Former User

      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 Reply Quote 0
      • A
        andre last edited by

        Did you run moc on it?

        1 Reply Last reply Reply Quote 1
        • ?
          Guest last edited by

          [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 Reply Quote 0
          • Q
            qxoz last edited by

            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 Reply Quote 0
            • sierdzio
              sierdzio Moderators last edited by

              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 Reply Quote 5
              • A
                andre last edited by

                [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 Reply Quote 0
                • ?
                  Guest last edited by

                  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 Reply Quote 0
                  • A
                    andre last edited by

                    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 Xu 1 Reply Last reply Reply Quote 1
                    • ?
                      Guest last edited by

                      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 Reply Quote 0
                      • I
                        Ivanov228 Banned @sierdzio last edited by

                        This post is deleted!
                        1 Reply Last reply Reply Quote 0
                        • E
                          Edward_Abrosimov last edited by

                          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 Reply Quote 0
                          • Jing Xu
                            Jing Xu @andre last edited by

                            @andre

                            Thanks. It's helpful.

                            1 Reply Last reply Reply Quote 0
                            • A
                              Asheesh last edited by

                              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_1980 1 Reply Last reply Reply Quote 0
                              • aha_1980
                                aha_1980 Lifetime Qt Champion @Asheesh last edited by

                                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 Reply Quote 1
                                • First post
                                  Last post