Results 1 to 7 of 7

Thread: Problem with the Qt docs

  1. #1
    Join Date
    Feb 2006
    Posts
    209
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Problem with the Qt docs

    Hi.
    I try to compile the example of the QObject derived class "Counter" on
    http://doc.trolltech.com/4.1/signalsandslots.html
    but I only get this error:

    Qt Code:
    1. g++ -c -pipe -O2 -march=athlon-xp -pipe -Wall -W -D_REENTRANT -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/doc/qt-4.1.0-r2/mkspecs/linux-g++ -I. -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4 -I. -I. -I. -o q.o q.cpp
    2. g++ -o snor q.o -L/usr/lib/qt4 -lQtGui -L/usr/lib/mysql -L/usr/lib -L/usr/lib/qt4 -laudio -lXt -lpng -lSM -lICE -lXi -lXrender -lXrandr -lXcursor -lfreetype -lfontconfig -lXext -lX11 -lQtCore -lz -lm -ldl -lpthread
    3. q.o: In function `main':
    4. q.cpp:(.text+0x4b): undefined reference to `vtable for Counter'
    5. q.cpp:(.text+0x69): undefined reference to `vtable for Counter'
    6. q.cpp:(.text+0xbb): undefined reference to `vtable for Counter'
    7. q.cpp:(.text+0xcd): undefined reference to `vtable for Counter'
    8. q.cpp:(.text+0xe8): undefined reference to `vtable for Counter'
    9. q.o:q.cpp:(.text+0xf7): more undefined references to `vtable for Counter' follow
    10. q.o: In function `Counter::setValue(int)':
    11. q.cpp:(.text+0x16): undefined reference to `Counter::valueChanged(int)'
    12. collect2: ld returned 1 exit status
    13. make: *** [snor] Error 1
    To copy to clipboard, switch view to plain text mode 

    What's wrong?
    Here is the code:

    Qt Code:
    1. #include <QObject>
    2.  
    3. class Counter : public QObject
    4. {
    5. Q_OBJECT
    6. public:
    7. Counter() { m_value = 0; }
    8. int value() const { return m_value; }
    9. public slots:
    10. void setValue(int value);
    11. signals:
    12. void valueChanged(int newValue);
    13. private:
    14. int m_value;
    15. };
    16.  
    17.  
    18. void Counter::setValue(int value)
    19. {
    20. if (value != m_value) {
    21. m_value = value;
    22. emit valueChanged(value);
    23. }
    24. }
    25.  
    26.  
    27. int main()
    28. {
    29. Counter a, b;
    30. QObject::connect(&a, SIGNAL(valueChanged(int)),
    31. &b, SLOT(setValue(int)));
    32. a.setValue(12); // a.value() == 12, b.value() == 12
    33. b.setValue(48); // a.value() == 12, b.value() == 48
    34. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem with the Qt docs

    Separate the class header and implementation (put them into separate files). And call superclass constructor in the constructor of the derived class.

  3. #3
    Join Date
    Feb 2006
    Posts
    209
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with the Qt docs

    Why would a header/implementation separation solve the problem? Or is that just a magic MOC thing that I have to accept?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem with the Qt docs

    Quote Originally Posted by Morea
    Why would a header/implementation separation solve the problem? Or is that just a magic MOC thing that I have to accept?
    Yes. moc won't work properly without it. There is a workaround for this, but it's not worth the effort.

  5. #5
    Join Date
    Feb 2006
    Location
    Österreich
    Posts
    35
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Problem with the Qt docs

    Your thread subject is "Problem with the Qt docs." I actually found something in the docs the other day when trying to find an answer for somebody else that was like, "We cannot stress enough that the most common bug people find in their software is an error about missing vtables. This 99% of the time means you forgot to run the moc on your code."

    I can't seem to find it now, but I found this instead:

    Quote Originally Posted by Qt 4.1 Reference
    Diagnostics

    moc will warn you about a number of dangerous or illegal constructs in the Q_OBJECT class declarations.

    If you get linkage errors in the final building phase of your program, saying that YourClass::className() is undefined or that YourClass lacks a vtable, something has been done wrong. Most often, you have forgotten to compile or #include the moc-generated C++ code, or (in the former case) include that object file in the link command. If you use qmake, try rerunning it to update your makefile. This should do the trick.
    Why would a header/implementation separation solve the problem? Or is that just a magic MOC thing that I have to accept?
    Because MOC doesn't look at your .cpp files for Q_OBJECT unless you tell it to. Under normal circumstances it only cares about the .h files. From the manual for MOC, however, is the following:

    For Q_OBJECT class declarations in implementation (.cpp) files, we suggest a makefile rule like this:

    foo.o: foo.moc

    foo.moc: foo.cpp
    moc -i $< -o $@

    This guarantees that make will run the moc before it compiles foo.cpp. You can then put

    #include "foo.moc"

    at the end of foo.cpp, where all the classes declared in that file are fully known.
    So it's not impossible, it's just not as easy as keeping your declarations separate from the implementation and doing "qmake -project ; qmake ; make." I think actually in many of the examples with the docs, there is a piece of code in main.cpp that says "#include main.moc" at the bottom. Is it in your Counter example or no? That is what this line means.
    My philosophy is: If you can use a free, open-source alternative: do it. And if you can't, pretend it's free and open-source and hope you don't get caught.

  6. #6
    Join Date
    Feb 2006
    Location
    Kirovohrad, Ukraine
    Posts
    72
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with the Qt docs

    Quote Originally Posted by Morea
    Why would a header/implementation separation solve the problem? Or is that just a magic MOC thing that I have to accept?
    Yes It is!

  7. #7
    Join Date
    Feb 2006
    Posts
    209
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with the Qt docs

    Thank you all for your answers.

Similar Threads

  1. Problem in using QHttp with QTimer
    By Ferdous in forum Newbie
    Replies: 2
    Last Post: 6th September 2008, 12:48
  2. Weird problem: multithread QT app kills my linux
    By Ishark in forum Qt Programming
    Replies: 2
    Last Post: 8th August 2008, 09:12
  3. Steps in solving a programming problem?
    By triperzonak in forum General Programming
    Replies: 8
    Last Post: 5th August 2008, 08:47
  4. problem with paint and erase in frame
    By M.A.M in forum Qt Programming
    Replies: 9
    Last Post: 4th May 2008, 20:17
  5. Replies: 16
    Last Post: 7th March 2006, 15:57

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.