Results 1 to 6 of 6

Thread: Forward declaration fails

  1. #1
    Join Date
    Jun 2011
    Posts
    6
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Forward declaration fails

    Hi guys, I got a little Problem with a forward declaration. It may be solved easily, but I don't find my mistake.

    I got a class TrackElement that has exactly the following header file.

    Qt Code:
    1. #ifndef TRACKELEMENT_H
    2. #define TRACKELEMENT_H
    3.  
    4. #include <QLabel>
    5.  
    6. #include "tracktest.h"
    7.  
    8. class TrackTest;
    9.  
    10. class TrackElement : public QLabel
    11. {
    12. Q_OBJECT
    13. public:
    14. explicit TrackElement(TrackTest *parent = 0);
    15.  
    16. private:
    17. TrackTest test;
    18. };
    19.  
    20. #endif // TRACKELEMENT_H
    To copy to clipboard, switch view to plain text mode 

    First, when I didn't use the forward declaration, the constructor had problems with it too, because im using the TrackElement to create TrackTest and vice versa.
    With the forward declaration the constructor causes no errors, but my private test variable of type TrackTest does. "field 'test' has incomplete type"

    I don't get it why, if I create a new TrackTest file in the TrackElement .cpp-file, there is no error at all.

    Thanks for fast answers

  2. #2
    Join Date
    Oct 2010
    Location
    Berlin, Germany
    Posts
    358
    Thanks
    18
    Thanked 68 Times in 66 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Forward declaration fails

    you have TrackTest as member element. Then you have to include the header. When you only have a forward declaration, how should the compiler know the size of TrackTest?

    if you insist on forward-declaring TrackTest, use a pointer as member.

    edit: fast enough?

  3. #3
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Forward declaration fails

    What do you mean by this:
    Qt Code:
    1. #include "tracktest.h"
    2.  
    3. class TrackTest;
    To copy to clipboard, switch view to plain text mode 
    I assume that TrackTest is defined in tracktest.h, so whats the point of class declaration after including its definition ? When you include tracktest.h, TrackTest is already a known class, so you can just remove the class TrackTest; declaration and all should work ok.
    If you want to use forward-declaration, dont include tracktest.h in header file, but in .cpp file, and leave only the class TrackTest; forward declaration in header (and use pointers). The way its done now makes no sense.

  4. The following user says thank you to stampede for this useful post:

    [?]jNs (27th July 2011)

  5. #4
    Join Date
    Jun 2011
    Posts
    6
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Forward declaration fails

    Huh, damn that was very fast
    Thanks to both of you. Now it works perfectly and I see, that it makes no sense to include it and then make a forward declaration.
    In general: is it "better" make forward declarations in the header file and include the .h in the sourcefile? Or should I use this way only if I got problems like the one above?
    Thanks again!

  6. #5
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Forward declaration fails

    Forward-declaring may reduce compilation time in case when you change the forward-declared class, because only the .cpp files which includes the class header "class.h" will be compiled again. If you include the "class.h" in header "a.h" instead of forward-declaring, then also every file that includes "a.h" will be recompiled (as well as all other files that include "class.h").
    Personally, I'm using forward declarations whenever its possible.
    ---
    Of course, using forward declaration forces you to use pointers if you want to use forward-declared class as member, so its up to you when you find it useful.
    Last edited by stampede; 27th July 2011 at 10:43. Reason: updated contents

  7. The following user says thank you to stampede for this useful post:

    [?]jNs (27th July 2011)

  8. #6
    Join Date
    Jun 2011
    Posts
    6
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Forward declaration fails

    Ok, thank you. That was the answer I needed

Similar Threads

  1. Forward Declaration of struct QCloseEvent ????
    By drake1983 in forum Newbie
    Replies: 2
    Last Post: 3rd February 2009, 15:16
  2. problem with forward declaration
    By MarkoSan in forum General Programming
    Replies: 14
    Last Post: 6th January 2008, 21:45
  3. Forward Class declaration ERROR
    By nleverin in forum Qt Programming
    Replies: 1
    Last Post: 30th July 2007, 08:35
  4. Forward declaration with std namespace
    By Raistlin in forum General Programming
    Replies: 2
    Last Post: 5th March 2007, 20:45
  5. Why forward declaration ?
    By probine in forum General Programming
    Replies: 3
    Last Post: 26th January 2007, 18:22

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.