Thread: Struct inside a Struct inside a Struct?!?

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    93

    Question Struct inside a Struct inside a Struct?!?

    i seem to be having problems with some code.. and to be honest i can't figure out why... i won't post my code but i'll post something similar...

    Code:
    // One.h
    
    #ifndef ONE_H
    #define ONE_H
    
    #include "Two.h"
    
    struct One
    {
      int stuff;
      One *next;
      Two *list;
    }
    
    #endif
    Code:
    // Two.h
    
    #ifndef TWO_H
    #define TWO_H
    
    #include "One.h"
    
    struct Two
    {
      unsigned int otherStuff;
      Two *next;
      One *back; // THis is where the error occurs.
    }
    
    #endif
    What i do is make a pointer to a One struct, however the error pops up at where i designated.. it basically won't let me define a [b]One[b] inside a Two struct.. why can't i make a struct point to another struct and that struct point back to the first type of struct... (making a linked list of linked lists)

  2. #2
    Open to suggestions Brighteyes's Avatar
    Join Date
    Mar 2003
    Posts
    204
    You may have to use a forward declaration:
    Code:
    struct B; // Make the name B known
    
    struct A {
        A* a;
        B* b;
    };
    
    struct B {
        A* a;
        B* b;
    };
    p.s. What the alphabet would look like without q and r.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    24
    You might need to typedef the structs, or at least refer to them as structs.
    Code:
    struct One
    {
      int stuff;
      struct One *next;
      struct Two *list;
    }
    Code:
    struct Two
    {
      unsigned int otherStuff;
      struct Two *next;
      struct One *back; 
    }
    Other suggestions are to not put them in different header files, since they are dependent on each other. I don't know if you can declare variables as externs in C++ or not, but that could also help.

  4. #4
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    This is C++ so no, the struct keyword isn't necessary even when you didn't do the typedef trick (someone pointed that out to me recently).

    also, since it's C++ we should be using classes anyway.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    28
    Greetings,

    C++ is all about classes! Besides, structs are classes in C++, the only difference (apperently) is that in struct's the members default to public where in classes it defaults to private.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    24
    Ah, yes. It's been a while since I've programmed anything with classes, so I kinda forgot what I was doing on for a second. Anyway, I don't know exactly what the error message says, but I think Brighteyes is on the right track with having to somehow delcare the struct as a known variable beforehand.

    As an aside: Even though something may not be necessary, my experience tells me that programs sometimes work better if it's done anyway.

  7. #7
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    syntax changes don't make programs run different. logic changes do.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  8. #8
    Registered User
    Join Date
    Feb 2002
    Posts
    93

    ah

    ah quick replies.. nice.. i'll try some of ur guys suggestions and see if i can get it to work...

    yeah i tried to put both structs in same file but since i never used forward declarations before it makes it kinda hard :P .. i gotta remember forward decs'... could be useful... yeah there is externs in c++ too.. might give that a whirl as well..

    i've never seen any code written where u refer to the datatypes by structs struct One *next; but if that works that could be pretty cool too..



    i'lll get back to ya'll if i run into more problems... thnx for the help

  9. #9
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297

    Re: ah

    Originally posted by tegwin
    i've never seen any code written where u refer to the datatypes by structs struct One *next; but if that works that could be pretty cool too..
    that's the way it had to be done in C
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 02:14 AM
  2. Converting from C to C++
    By Taka in forum C++ Programming
    Replies: 5
    Last Post: 04-08-2009, 02:16 AM
  3. Looking for a way to store listbox data
    By Welder in forum C Programming
    Replies: 20
    Last Post: 11-01-2007, 11:48 PM
  4. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  5. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM