CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Nov 2007
    Posts
    14

    Question structs pointing to each other; compile problem

    Hei!

    I have defined two structure types. Each has a parameter that is a pointer to the other structure, and, of course, it does not compile. How can I come over this problem? This structure definition will result in a very efficient coding, so I would like to keep it.
    Example:

    typedef struct Item_s{
    int param1;
    int param2;
    std::list<EntryList_T::iterator> entryPtrList;
    }Item_T;

    typedef std::list<Item_T> ItemList_T;

    typedef struct Entry_s{
    uint id;
    ItemList_T::iterator item_iter;
    }Entry_T;

    typedef std::list<Entry_T> EntryList_T;

  2. #2
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: structs pointing to each other; compile problem

    Your answer is forward referencing.

    Code:
    // struct B is referenced
    
    // this tells the compiler there is a struct B, but the compiler doesn't know
    // what's in there. The compiler will resolve the content of struct B later.
    typedef struct B; 
    
    
    typedef struct _A
    {
       B *ptrToStructB;
    }A;
    
    typedef struct _B
    {
       A *ptrToStructA;
    }B;

  3. #3
    Join Date
    Nov 2007
    Posts
    14

    Re: structs pointing to each other; compile problem

    Hi!

    Thank you for your answer, but I cannot make it work. I get en error:

    error: invalid redeclaration of type name "Entry_T" (declared at line 159)


    Is this compiler dependent or ..?

  4. #4
    Join Date
    Oct 2002
    Posts
    1,134

    Re: structs pointing to each other; compile problem

    Skizmo made a typing error: the forward reference should be

    struct B;

    (without the "typedef").
    Regards
    Robert Thompson

  5. #5
    Join Date
    May 2002
    Posts
    1,435

    Re: structs pointing to each other; compile problem

    Forward referencing will not work unless you modify your code to use pointers as shown by Skizmo.

    But even if you do use pointers I believe that it will still be impossible to use *EntryList_T::interator as a template argument. In order to know that EntryList_T has an iterator the class must be previously defined - the same problem you have now.

    I would suggest re-thinking the problem to see if you can come up with a more elegant design for your application's data objects.

  6. #6
    Join Date
    Nov 2007
    Posts
    14

    Question Re: structs pointing to each other; compile problem

    Hello again and thank your for your help!

    But, ... I cannot make forward reference work even if I make as simple as the example your had:

    struct B;


    typedef struct _A
    {
    B *ptrToStructB;
    }A;

    typedef struct _B
    {
    A *ptrToStructA;
    }B;


    I still get th error:

    error: invalid redeclaration of type name "B" (declared at line 159)
    }B;

    So, is this something with the compiler (I use Intel 9.1) or a compiler option that I should change?

    Regards,

    Elise

  7. #7
    Join Date
    Mar 2003
    Location
    India {Mumbai};
    Posts
    3,871

    Re: structs pointing to each other; compile problem

    Try removing the 'typedef' altogether. You are 'forward' defining 'B' as plain struct (telling the compiler), and then you are using typedef keyword to define B (based on _B).
    My latest article: Explicating the new C++ standard (C++0x)

    Do rate the posts you find useful.

  8. #8
    Join Date
    Nov 2007
    Posts
    14

    Re: structs pointing to each other; compile problem

    Removing typedef helps.

    It compiles now.

    Thank you!

  9. #9
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: structs pointing to each other; compile problem

    [ moved thread ]
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured