Thread: Structs do not see each other

  1. #1
    Registered User
    Join Date
    Nov 2016
    Posts
    2

    Post Structs do not see each other

    I have two structs. One of them includes pointer member that points to other one. This situation is valid for both structs.

    For example;

    myHeader.h:

    Code:
    struct A{
        
        B* x;
    };
    
    struct B{
    
        A* y;
    };
    No matter what I do, A does not see B. Finally, I wrote struct B; at the top of the code, which eliminated the problem. Is this terminologically right ? Can you give another solution to me for this ?

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by goktug1514 View Post
    Finally, I wrote struct B; at the top of the code, which eliminated the problem. Is this terminologically right ? Can you give another solution to me for this ?
    That is correct. The compiler needs to see B, the type you're trying to use when you're declaring an instance of its type. Since the definition of B comes later, you have to tell the compiler it exists by forwarding declaring the struct.

    As for whether there's another solution or not, that's usually difficult to say unless we know what you're trying to do. One idea might be to try to find a common intersection of A and B and break that out into a class/struct C which both A and B can reference.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Nasal Demon Xupicor's Avatar
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    179
    Code:
    struct B;
    That's a declaration of B. It's also known as "forward declaration". At this point type B is incomplete, but you can use it to declare pointers to B or references to B (it's a tad more complicated with smart pointers... But it's possible).

  4. #4
    Registered User
    Join Date
    Nov 2016
    Posts
    2
    Thank you for your helps
    I handled it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 01-08-2013, 07:55 AM
  2. Typedef Structs inside Typdef structs
    By gremory in forum C Programming
    Replies: 21
    Last Post: 12-30-2011, 07:48 PM
  3. [ noob question ] Help with structs within structs
    By Riverfoot in forum C Programming
    Replies: 3
    Last Post: 04-26-2011, 07:24 PM
  4. Passing Structs Into An Array Of Structs.
    By TheTaoOfBill in forum C Programming
    Replies: 3
    Last Post: 10-07-2010, 09:38 AM
  5. passing structs & pointers to structs as arguments
    By Markallen85 in forum C Programming
    Replies: 6
    Last Post: 03-16-2004, 07:14 PM

Tags for this Thread