Thread: Class files including other classes

  1. #1
    Registered User combatdave's Avatar
    Join Date
    Nov 2005
    Posts
    20

    Class files including other classes

    Hiya, got a bit of a problem with some of my classes. Each class has its own files (such as MyClass.cpp and MyClass.h), and all is going swimmingly.

    My code is designed such that there is a class, AppClass, which contains all instances of the other classes - AgentClass and ItemClass. Inside my AgentClass, i need a function to which i can pass my instance of AppClass, as each AgentClass object will need to search for nearby Agents or Items. Below is exactly which files include which other files.


    (ObjectClass is the base class on which all objects inside the game (be it items or agents) are built upon.)


    Main.cpp:
    Code:
    #include "ObjectClass.h"
    #include "AgentClass.h"
    #include "ItemClass.h"
    #include "AppClass.h"

    AppClass.h:
    Code:
    #include "ObjectClass.h"
    #include "AgentClass.h"
    #include "ItemClass.h"
    AgentClass.h:
    Code:
    #include "ObjectClass.h"
    ItemClass.h:
    Code:
    #include "ObjectClass.h"
    Now, inside my AgentClass i have tried including AppClass.h in order to let me use an AppClass instance as a parameter for a function. The problem is, including AppClass throws up a stupid amount of errors, none of which even hint at how to remedy the problem.

    Any ideas? even ideas of what to google would be appreciated - i have tried already but its quite a hard problem to phrase in such a way that google understands.

    Cheers for your time.
    ( )

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Any ideas?
    Are you using inclusion guards? Do you have global data? There are a lot of things that can go wrong with this, though inclusion guards and namespaces can clear up most of them.
    My best code is written with the delete key.

  3. #3
    Registered User combatdave's Avatar
    Join Date
    Nov 2005
    Posts
    20
    im going to guess no to inclusion guards, not entirely sure what they are...

    i do have a small amount of global data, but that is held inside the main.cpp file, and nowhere else

    cheers for the speedy reply
    ( )

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> inside my AgentClass i have tried including AppClass.h
    If you meant AgentClass.h there, you will likely need to use forward declarations instead of including the AppClass header directly. Since AppClass.h already includes AgentClass.h, you cannot have AgentClass.h include AppClass.h, it becomes a circular include.

    When you only need to include a header because a type is used as a function parameter, or as a pointer or reference member in the class, you should use a forward declaration instead of including the entire header. This is true in general, and required when you have circular includes.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >im going to guess no to inclusion guards, not entirely sure what they are...
    Inclusion guards are just a preprocessor trick that makes sure the contents of a header are only included once, regardless of how deeply you nest them. It's a good idea for every header you make:
    Code:
    #ifndef SOMEUNIQUENAME
    #define SOMEUNIQUENAME
    
    // Your header stuff here
    
    #endif
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Generally people use the name of the file as part of the SOMEUNIQUENAME part. So you might use APPCLASS_H for your AppClass.h file.

  7. #7
    Registered User combatdave's Avatar
    Join Date
    Nov 2005
    Posts
    20
    ah yes, i am already using inclusion guards.

    il give the forward declaration gubbins a go shortly. if i understand correctly, its as simple as putting "class AppClass;" in?

    cheers.
    ( )

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> its as simple as putting "class AppClass;" in?
    Yes, and of course removing the #include "AppClass.h". You will need to #include "AppClass.h" in the cpp file, though, which you probably should have been doing anyway.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Anyone with a spare java class file(s) with nested classes?
    By Sebastiani in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 04-29-2009, 08:33 PM
  3. How to use classes in seperate C++ files
    By dxfoo in forum C++ Programming
    Replies: 3
    Last Post: 03-19-2006, 05:55 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM