Results 1 to 3 of 3

Thread: Headers Mutually Inclusive

  1. #1
    Join Date
    Feb 2014
    Posts
    23
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Headers Mutually Inclusive

    I know in general it is a bad practice to have two class declarations that are dependent upon each other, but it seems to be the only way to achieve what I'm attempting.

    I have a class GameScene that is inheriting from QGraphicsScene. I have another class Troop, that is inheriting from QGraphicsObject.

    GameScene does not have any members that are of type troop, just some signal/slot connections and occurrences where it will be passed an object of type Troop and call its public member functions.

    Troop needs to have a pointer to GameScene in order to call a member function from GameScene. Unfortunately, I cannot use the scene() function from the QGraphicsItem library as the function does not exist on a typical QGraphicsScene.

    This whole situation is leading to where I cannot include "troop.h" in GameScene and "gamescene.h" in Troop because it creates an infinite loop, and forward declaring either of the classes in the other class will not allow for access to member functions, so is there another work-around to this situation?

    Thanks.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Wiki edits
    17

    Default Re: Headers Mutually Inclusive

    You do not need a complete declaration, i.e. The header, for a class in order to declare a pointer to an object of that class. You can use a simple forward declaration in each header for the class:
    Qt Code:
    1. class ClassA;
    2.  
    3. class ClassB {
    4. ...
    5. void doStuff(ClassA *classA);
    6. ...
    7. }
    To copy to clipboard, switch view to plain text mode 

    You will need the complete header for ClassA included in the cpp file that provides the implementation of ClassB.
    Last edited by ChrisW67; 1st March 2014 at 06:37. Reason: updated contents

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

    squeegedog (1st March 2014)

  4. #3
    Join Date
    Feb 2014
    Posts
    23
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: Headers Mutually Inclusive

    You will need the complete header for ClassA included in the cpp file that provides the implementation of ClassB.
    That's the part I was missing, thanks

Similar Threads

  1. Where are those headers?
    By yagabey in forum Qt for Embedded and Mobile
    Replies: 5
    Last Post: 17th January 2009, 17:29
  2. Precompiled headers
    By Benjamin in forum Qt Programming
    Replies: 1
    Last Post: 4th December 2008, 15:48
  3. UI headers does not compile
    By bnilsson in forum Qt Tools
    Replies: 1
    Last Post: 30th October 2008, 09:16
  4. Installing headers
    By MathStuf in forum Qt Programming
    Replies: 2
    Last Post: 30th June 2008, 12:05
  5. importing headers
    By peace_comp in forum Qt Programming
    Replies: 1
    Last Post: 29th March 2008, 23:35

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
  •  
Qt is a trademark of The Qt Company.