Results 1 to 7 of 7

Thread: Forward declarations needed for some?

  1. #1
    Join Date
    Aug 2007
    Location
    Gothenburg, Sweden
    Posts
    9
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Forward declarations needed for some?

    Hello.

    This is a question to still my curiosity about forward declarations. According to Blanchette and Summerfield's book, forward declarations is needed only to make compilation "somewhat faster". But I just got problems for not having them onboard in my program.
    Qt Code:
    1. incomehandler.h:21: error: ISO C++ forbids declaration of `QLabel' with no type
    2. incomehandler.h:21: error: expected `;' before '*' token
    To copy to clipboard, switch view to plain text mode 

    I commented after the declaration in question in the code below, and since the compiler doesn't go on strike now even if I still haven't forward-declared the QPushButton.

    Why is The QLabel more sensitive than QPushButton?

    Qt Code:
    1. #ifndef INCOMEHANDLER_H
    2. #define INCOMEHANDLER_H
    3.  
    4. #include <QDialog>
    5.  
    6. class QLabel; //With this row my prorgam compiles, without it i get the error posted above.
    7.  
    8. class IncomeHandler : public QDialog
    9. {
    10. Q_OBJECT
    11.  
    12. public:
    13. IncomeHandler(QWidget *parent = 0);
    14. signals:
    15.  
    16. private slots:
    17. void okClicked();
    18.  
    19. private:
    20. QLabel *activeLabel;
    21. QPushButton *okButton;
    22.  
    23. };
    24.  
    25. #endif
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Oct 2007
    Location
    Munich, Bavaria
    Posts
    144
    Thanks
    1
    Thanked 19 Times in 19 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Forward declarations needed for some?

    just have a look into the headers included QPushButton is foreward declared already.
    QLabel is not.

  3. The following user says thank you to DeepDiver for this useful post:

    doktorn (26th November 2007)

  4. #3
    Join Date
    Aug 2007
    Location
    Gothenburg, Sweden
    Posts
    9
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Forward declarations needed for some?

    OK. So it seems forward declarations are necessary always. Thanks!

  5. #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: Forward declarations needed for some?

    No. Forward declaring a class is an alternative to including its header file. In that meaning forward declarations improve the compilation speed compared to including a header file.

  6. #5
    Join Date
    Aug 2007
    Location
    Gothenburg, Sweden
    Posts
    9
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Forward declarations needed for some?

    OK. So when should I rather include the header file than making a forward declaration?

  7. #6
    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: Forward declarations needed for some?

    When you need to know the size of the class defined in the header file. You can only forward declare classes you will be using as pointers. If you want to have an object, you need a full definition:
    Qt Code:
    1. class X {
    2. };
    To copy to clipboard, switch view to plain text mode 
    In this situation you can forward declare QPushButton (because the size of the pointer is known so you don't need to know the size of the complete object) but you can't forward declare QLabel. Of course you also need full definitions (hence include the header) if you want to use methods of the class in question (for example in implementation files).

  8. The following user says thank you to wysota for this useful post:

    doktorn (1st December 2007)

  9. #7
    Join Date
    Jan 2006
    Location
    Earth (Terra)
    Posts
    87
    Thanks
    4
    Thanked 6 Times in 4 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: Forward declarations needed for some?

    You basically need to include the header if the code needs to know anything about the class.

    Otherwise, you can just do a forward declaration.

    This applies to header or source files.

    Qt Code:
    1. #include "Y.h"
    2. #include "Z.h"
    3. // includes
    4.  
    5. class W;
    6. // forward declaration
    7.  
    8. class X
    9. {
    10. public:
    11.  
    12. Y y;
    13. // embedded class - you need the header.
    14.  
    15. W *w;
    16. // pointer - you *may* need the header
    17.  
    18. static Z *z;
    19. // singleton - we'll need the header, because we're going to
    20. // allocate an instance.
    21.  
    22. Z *getZ()
    23. {
    24. if (!z)
    25. z = new Z();
    26. // (here's the allocation)
    27.  
    28. return z;
    29. }
    30.  
    31. void doSomethingWithZMember()
    32. {
    33. getZ();
    34. doSomething(z->member);
    35. // we need a header, because we're using a member.
    36. // this would be true whether we allocated z here or not.
    37. }
    38.  
    39. void doSomethingWithWZ()
    40. {
    41. doSomething(w);
    42. doSomething(getZ());
    43. // don't need headers to just pass pointers around.
    44. // (but in this instance, we need the Z header to do the above
    45. // ops)
    46. }
    47.  
    48. void doSomething(int){}
    49. void doSomething(Z *){}
    50. void doSomething(W *){}
    51. };
    To copy to clipboard, switch view to plain text mode 

    If the function bodies were moved down into the .cpp files (including the accessor), you could use forward declarations for Z (but not Y). You probably wouldn't, though, because X is intimately tied with Z...

    It depends.

    rickb
    Last edited by rickbsgu; 28th November 2007 at 09:11.

  10. The following 2 users say thank you to rickbsgu for this useful post:

    doktorn (1st December 2007), vonCZ (28th November 2007)

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.