Qt Forum

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

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    Signal/Slot. Error during compilation.

    General and Desktop
    2
    3
    987
    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.
    • W
      wf778899 last edited by

      Hi to all! Recently i have start to learn Qt and now i try to use signals and slots in a simple user widget taked from the example in Qt Assistance (listing below). But it can't build in, error messages are appears in lines 3, 6, 19, 27, 241 (its deskriptions are below the listing...). I uses Qt 5.3.1. Can anybody help me?
      @#include <QApplication>
      #include <QObject>
      class Counter : public QObject{
      Q_OBJECT
      public:
      Counter() { m_value = 0; }
      int value() const { return m_value; }
      public slots:
      void setValue(int value);
      signals:
      void valueChanged(int newValue);
      private:
      int m_value;
      };

      void Counter::setValue(int value) {
      if (value != m_value) {
      m_value = value;
      emit valueChanged(value);
      }
      }

      int main(int argc, char *argv[]) {
      QApplication app(argc, argv);
      Counter a, b;
      QObject::connect(&a, &Counter::valueChanged, &b, &Counter::setValue);
      a.setValue(12); // a.value() == 12, b.value() == 12
      b.setValue(48); // a.value() == 12, b.value() == 48
      return app.exec();
      }
      @

      Line 19 D:\Projects\QT\qqq\main.cpp:19: error: undefined reference to Counter::valueChanged(int)' Line 27 D:\Projects\QT\qqq\main.cpp:27: error: undefined reference to Counter::valueChanged(int)'
      Line 06 D:\Projects\QT\qqq\main.cpp:6: error: undefined reference to vtable for Counter' Line 241 C:\Qt\5.3\mingw482_32\include\QtCore\qobject.h:241: error: undefined reference to Counter::staticMetaObject'
      Line 03 D:\Projects\QT\qqq\main.cpp:3: error: undefined reference to `vtable for Counter'
      collect2.exe:-1: error: error: ld returned 1 exit status

      1 Reply Last reply Reply Quote 0
      • Chris Kawa
        Chris Kawa Moderators last edited by

        Hi, welcome to devnet.

        MOC (the meta object compiler) reads only headers. Your class declaration (with Q_OBJECT macro) is in a .cpp file so it won't generate the QObject stuff (the undefined symbols you're getting).
        Put your class in a header where it belongs and it should compile fine.

        1 Reply Last reply Reply Quote 0
        • W
          wf778899 last edited by

          I had already guessed)) Fanks a lot for reply!)

          1 Reply Last reply Reply Quote 0
          • First post
            Last post