Results 1 to 3 of 3

Thread: Include issue

  1. #1
    Join Date
    Sep 2009
    Posts
    9
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Question Include issue

    Hi. I have a short test program and i need help concerning include files.
    here is the code:

    Qt Code:
    1. #ifndef CPROTOCOL_H
    2. #define CPROTOCOL_H
    3.  
    4. #include "cpacket.h"
    5.  
    6. class CProtocol
    7. {
    8. private:
    9. CPacket *mPacketList;
    10.  
    11. public:
    12. CProtocol();
    13. };
    14.  
    15. #endif // CPROTOCOL_H
    16.  
    17. #ifndef CPACKET_H
    18. #define CPACKET_H
    19.  
    20. #include "cprotocol.h"
    21.  
    22. class CPacket
    23. {
    24. private:
    25. CProtocol *parent;
    26.  
    27. public:
    28. CPacket();
    29. };
    30.  
    31. #endif // CPACKET_H
    To copy to clipboard, switch view to plain text mode 

    The compiler exits with return value 2 and says "In file included from cprotocol.h:4,"

    I know that it has something to do with the recursive including of the two files but i think the #ifndef should prevent this.....
    i hope someone can tell me what i'm doing wrong

  2. #2
    Join Date
    Sep 2009
    Posts
    140
    Thanks
    4
    Thanked 17 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Include issue

    You have to use forward declaration.

    The compiler read cprotocol.h,
    then cpacket.h through include directive,
    then cprotocol again through include directive, but skip it because of #ifndef
    then read CPacket definition and CPtrotocol is not defined!

    Just declare the class without defining it instead of #include directive:

    Qt Code:
    1. #ifndef CPROTOCOL_H
    2. #define CPROTOCOL_H
    3.  
    4. class CPacket; //forward declaration
    5. class CProtocol
    6. {
    7. private:
    8. CPacket *mPacketList;
    9.  
    10. public:
    11. CProtocol();
    12. };
    13.  
    14. #endif // CPROTOCOL_H
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #ifndef CPACKET_H
    2. #define CPACKET_H
    3.  
    4. class CProtocol; //forward declaration
    5. class CPacket
    6. {
    7. private:
    8. CProtocol *parent;
    9.  
    10. public:
    11. CPacket();
    12. };
    13.  
    14. #endif // CPACKET_H
    To copy to clipboard, switch view to plain text mode 

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

    Platoon (6th October 2009)

  4. #3
    Join Date
    Sep 2009
    Posts
    9
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Include issue

    Thank you for the fast help....this works fine and now i will always make forward declarations in headers.

Similar Threads

  1. Replies: 0
    Last Post: 4th August 2009, 15:24
  2. segmentation fault on closing
    By harakiri in forum Qt Programming
    Replies: 6
    Last Post: 8th July 2009, 13:54
  3. Error: Using Multiple files
    By 3nc31 in forum Qt Programming
    Replies: 1
    Last Post: 22nd November 2007, 09:23
  4. use button from another Window
    By raphaelf in forum Qt Programming
    Replies: 11
    Last Post: 2nd March 2006, 20:31
  5. [QT4 & XP] QTreeView issue with Designer form
    By incapacitant in forum Newbie
    Replies: 3
    Last Post: 2nd March 2006, 17:42

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.