Results 1 to 8 of 8

Thread: C++ List of alternating structures

  1. #1
    Join Date
    Oct 2009
    Beans
    83

    C++ List of alternating structures

    In a certain .hpp of mine, I would like to define a list of two alternating kind of structures:

    Code:
    struct A
    {
        /* TONS of stuff */
        B* Next;
        B* Previous;
    };
    
    struct B
    {
        /* very few stuff */
        A* Next;
        A* Previous;
    };
    The problem, the way I wrote this code, is that the compiler has no idea of what B is when it finds it while reading the definition of struct A. With functions the problem is solved by pre-declaring them at the beginning of the file or by declaring the header.

    How do I do it (without putting everything in a single structure so that the problem is avoided altogheter)?
    Last edited by Dirich; April 2nd, 2013 at 09:46 AM.

  2. #2
    Join Date
    May 2007
    Location
    Leeds, UK
    Beans
    1,664
    Distro
    Ubuntu 13.10 Saucy Salamander

    Re: C++ List of alternating structures

    I'm not a C++ expert but I happened to be reading about this the other day. It should work if you declare it like this:

    Code:
    struct A {
        struct B *next;
        struct B *prev;
    };
    
    struct B {
        struct A *next;
        struct A *prev;
    };
    Gory details here ...

    http://stackoverflow.com/questions/6...ef-struct-in-c

    Someone who knows the ins and outs of this better than I do will fill in the details.
    Please create new threads for new questions.
    Please wrap code in code tags using the '#' button or enter it in your post like this: [code]...[/code].

  3. #3
    Join Date
    Oct 2009
    Beans
    83

    Re: C++ List of alternating structures

    Quote Originally Posted by r-senior View Post
    I'm not a C++ expert but I happened to be reading about this the other day. It should work if you declare it like this:

    Code:
    struct A {
        struct B *next;
        struct B *prev;
    };
    
    struct B {
        struct A *next;
        struct A *prev;
    };
    Gory details here ...

    http://stackoverflow.com/questions/6...ef-struct-in-c

    Someone who knows the ins and outs of this better than I do will fill in the details.

    Thank you. That link is very useful. I did not know that of the tag and typedef namespace.
    The solution is the same as in the case I want struct A to contain a pointer to struct A!

  4. #4
    Join Date
    Apr 2013
    Beans
    2

    Re: C++ List of alternating structures

    If you are using C++, some of this can be avoided by using Classes. and namespaces.

  5. #5
    Join Date
    Mar 2006
    Beans
    837

    Re: C++ List of alternating structures

    Quote Originally Posted by Dirich View Post
    In a certain .hpp of mine, I would like to define a list of two alternating kind of structures:

    Code:
    struct A
    {
        /* TONS of stuff */
        B* Next;
        B* Previous;
    };
    
    struct B
    {
        /* very few stuff */
        A* Next;
        A* Previous;
    };
    The problem, the way I wrote this code, is that the compiler has no idea of what B is when it finds it while reading the definition of struct A. With functions the problem is solved by pre-declaring them at the beginning of the file or by declaring the header.

    How do I do it (without putting everything in a single structure so that the problem is avoided altogheter)?
    I think another way of solving this is with "forward declaration" of the struct/class. Warning I didn't actually try to compile the following code:
    Code:
    struct B; //forward declare the struct
    
    struct A
    {
        /* TONS of stuff */
        B* Next;
        B* Previous;
    };
    
    struct B
    {
        /* very few stuff */
        A* Next;
        A* Previous;
    };

  6. #6
    Join Date
    Oct 2009
    Beans
    83

    Re: C++ List of alternating structures

    Namespaces is what the solution suggested in the link by the 1st one to answer is about: tag namespace vs typedef namespace. The typedef namespace allows the forward declaration of the struct. So, maybe you last 2 have in mind different things, or maybe everybody got the same idea!

  7. #7
    Join Date
    Apr 2008
    Beans
    221

    Re: C++ List of alternating structures

    Bravo. I couldn't believe this went through 5 comments without someone suggesting forward declaration. In essence this is a very common problem to have, and until you get familiar with forward declaration it becomes PITA to dance around it.

  8. #8
    Join Date
    Mar 2006
    Beans
    837

    Re: C++ List of alternating structures

    @akvino read it again. I already mentioned forward declaration.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •