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

  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
    28,413
    You can use a forward declaration:
    Code:
    #ifndef ACTION_H
    #define ACTION_H
    
    class Unit;
    
    class Action {
      Unit *target_unit;
      int action_type;
    };
    #endif
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    kroiz, why do you recommend that the vector should store Action pointers?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    Haha, no worries, I lost a "fastest finger first" reply race recently to two people
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 12:52 PM
  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-22-2002, 12:30 AM