Signal/Slot. Error during compilation.

  • 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{
    Counter() { m_value = 0; }
    int value() const { return m_value; }
    public slots:
    void setValue(int value);
    void valueChanged(int newValue);
    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 toCounter::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 toCounter::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

  • Moderators

    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.

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

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.