Thread: class prototypes for cross-referencing

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    40

    class prototypes for cross-referencing

    The compiler yells at me when I try to make use of a definition in the code above where it is defined.
    Code:
    class type1
    {
      type2 next;  //It hasn't seen type2 yet, so it shouts at me here
    };
    
    class type2
    {
      type1 previous;
    };
    Is there a way to prototype a class before you define it, like there is with functions, so that I can let the compiler know that type2 is there for type1?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    You can't have classes which have mutually recursive members.

    But you can have pointers to mutually recursive members.
    Code:
    class type2;  // a forward declaration for a class
    class type1
    {
      type2 *next;  //It hasn't seen type2 yet, so it shouts at me here
    };
    
    class type2
    {
      type1 *previous;
    };
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    40
    thanks

  4. #4
    Registered User
    Join Date
    Mar 2006
    Posts
    40
    It got rid of the compiler error at first, but now that I try to actually use it, it still yells at me. It tells me that my use of the undefined class is invalid. Here's what I'm trying to do more specifically.

    I have the classes person and room, and I want the person to have a variable referencing the room it is.
    Code:
    class person
    {
      public:
      room *loc;
      unsigned short int x, y, z;
      person(void)
      {
        x = 0; y = 0; z = 0;
        loc = &rooms[x][y][z];
        loc->enter();
      }
    }
    And for room.enter() I have
    Code:
    class room
    {
      public:
      void enter(PC *PC = NULL)
      {
        if(PC == NULL) PC = &you //you being a variable that keeps track of the main person
        PC->loc = this object
        //I don't know how to reference the source, but that's something I already had a different post asking about
      }
    }

  5. #5
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    You cannot define the functions that reference the pointers to the forward declared class inside the class. One way would be to define them in separate implementation files.
    If you still want to have them inline you could implement them after declaration of the classes using the inline keyword.
    e.g.
    Code:
    class type2;  // a forward declaration for a class
    class type1
    {
      type2 *next;  //It hasn't seen type2 yet, so it shouts at me here
      void do_sth_widh_next();
    };
    
    class type2
    {
      type1 *previous;
      void set_prev( type1 * a ){
         previous= a;
      }
    };
    
    inline void type1::do_sth_widh_next() {
      next->set_prev( this );
    }
    Kurt
    Last edited by ZuK; 03-19-2006 at 03:21 PM.

  6. #6
    Registered User
    Join Date
    Mar 2006
    Posts
    40
    It all seems to be working now. Thanks much.

    Is there any way to tag a post as solved on this board? I know there are on some, and I'm just wondering so I have a way to let people know it's solved without needing to bump the post up.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > Is there any way to tag a post as solved on this board?
    Nope - people just say thanks and the post gets abandoned - if it's lucky.

    People refer to it from time to time (if they can be bothered to search), and just occasionally, some moron will come along and bump threads some years later with a vacuous "me too" comment.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. class composition constructor question...
    By andrea72 in forum C++ Programming
    Replies: 3
    Last Post: 04-03-2008, 05:11 PM
  2. Creating a database
    By Shamino in forum Game Programming
    Replies: 19
    Last Post: 06-10-2007, 01:09 PM
  3. Need help to build network class
    By weeb0 in forum C++ Programming
    Replies: 0
    Last Post: 02-01-2006, 11:33 AM
  4. class errors
    By romeoz in forum C++ Programming
    Replies: 3
    Last Post: 09-16-2003, 07:57 PM