How do I define two C++ classes with pointers to each other?

This is a discussion on How do I define two C++ classes with pointers to each other? within the C++ Programming forums, part of the General Programming Boards category; I'm trying to make two classes that hold a pointer or variable of the other, like this (pseudo-code): ---unit.h--- Code: ...

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    5

    How do I define two C++ classes with pointers to each other?

    I'm trying to make two classes that hold a pointer or variable of the other, like this (pseudo-code):
    ---unit.h---
    Code:
    #ifndef UNIT_H
    #define UNIT_H
    #include "action.h"
    #include <vector>
    using namespace std;
    
    class Unit {
      vector<Action> actions;
      int hp;
    };
    #endif
    ---action.h---
    Code:
    #ifndef ACTION_H
    #define ACTION_H
    #include "unit.h"
    
    class Action {
      Unit *target_unit;
      int action_type;
    };
    #endif
    But since one is defined inside the other, it can't compile the program. It reads in action.h and sees unit.h is included, when it read unit.h, it says Action is not defined. Is there a way around error?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,758
    You can use a forward declaration:
    Code:
    #ifndef ACTION_H
    #define ACTION_H
    
    class Unit;
    
    class Action {
      Unit *target_unit;
      int action_type;
    };
    #endif
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User kroiz's Avatar
    Join Date
    Jun 2007
    Posts
    116
    you do a forward reference.


    Code:
    #ifndef UNIT_H
    #define UNIT_H
    class Action;
    #include <vector>
    using namespace std;
    
    class Unit {
      vector<Action*> actions;
      int hp;
    };
    #endif

    Code:
    #ifndef ACTION_H
    #define ACTION_H
    class Unit;
    
    class Action {
      Unit *target_unit;
      int action_type;
    };
    #endif
    notice that the vector now has a pointer to Action.
    the include will go into the cpp file.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,758
    kroiz, why do you recommend that the vector should store Action pointers?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jun 2007
    Posts
    5

    Thanks

    Thanks for the fast replies.

  6. #6
    Registered User kroiz's Avatar
    Join Date
    Jun 2007
    Posts
    116
    Quote Originally Posted by laserlight View Post
    kroiz, why do you recommend that the vector should store Action pointers?
    I think that if it is not a pointer then he can not use forward reference it must use include.
    I am pretty sure. Am I wrong?

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,758
    I think that if it is not a pointer then he can not use forward reference it must use include.
    I am pretty sure. Am I wrong?
    You are right, but the Action class has a Unit* member variable, so the forward declaration can be used there and action.h can continue to be included in unit.h, requiring no change to the vector.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User kroiz's Avatar
    Join Date
    Jun 2007
    Posts
    116
    yes ofcourse you are right.
    I was in such a hurry to post first because it is my first reply to someone on the forum that I missed the details.
    and you beat me to it after all.
    not fair )-:

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,758
    Haha, no worries, I lost a "fastest finger first" reply race recently to two people
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Massively Single Player AverageSoftware's Avatar
    Join Date
    May 2007
    Location
    Buffalo, NY
    Posts
    141
    Quote Originally Posted by kroiz View Post
    I think that if it is not a pointer then he can not use forward reference it must use include.
    I am pretty sure. Am I wrong?
    You're sort of wrong. Forward declarations can be used with references too.
    There is no greater sign that a computing technology is worthless than the association of the word "solution" with it.

  11. #11
    Registered User kroiz's Avatar
    Join Date
    Jun 2007
    Posts
    116
    I did not know that,That is good to know.
    thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. size of an integer pointer
    By onebrother in forum C Programming
    Replies: 5
    Last Post: 07-09-2008, 11:49 AM
  2. Accessing syscalls from C
    By lilcoder in forum C Programming
    Replies: 17
    Last Post: 09-19-2007, 02:27 PM
  3. LISP (DrScheme) any one?
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 03-31-2004, 11:52 AM
  4. keyboard control
    By MathFan in forum C++ Programming
    Replies: 1
    Last Post: 04-16-2002, 11:55 AM
  5. queued signals problem, C programming related
    By Unregistered in forum Linux Programming
    Replies: 3
    Last Post: 01-21-2002, 11:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21