Thread: Forward declaration of structs?

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    186

    Forward declaration of structs?

    Hello,

    My problem is that I have two structs that need to know about each other. Obviously just having them like this won't work:

    Code:
    struct Requester
    {
    	bool beingServiced;
    	list<Request> requests;
    };
    
    struct Request
    {
    	Requester r;
    	int track;
    };
    This doesn't compile. I think what I need is a "forward declaration" but I'm not sure what that is or how to do one, I just know that it will let me do this I think.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    struct Request;
    
    struct Requester
    {
    	bool beingServiced;
    	list<Request> requests;
    };
    
    struct Request
    {
    	Requester r;
    	int track;
    };
    you mean like that?
    I'm not sure that will work for whole objects and not pointers or referencies...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    It seems that incomplete types are not allowed to be used as list template argument.

    You may need to put Request before Requester, forward declare Requester and replace the r member with Requester* (why does request hold a requester by value?)
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User
    Join Date
    Jul 2007
    Posts
    186
    Figured it out thanks. First suggestion did the trick.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Is the first even legal? Unless, of course, list only references the struct via pointers or references, of course, and seeing as it is a std::list, that makes sense...
    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.

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    List holds copies of the instances if I'm not mistaken

    In any case, Comeau online says this:

    Code:
    "concept_checks.h", line 555: error: incomplete type is not allowed
        static void _Assignable_requirement_violation(_Type __a) {
                                                      ^
              detected during instantiation of "void
                        _Assignable_concept_specification<_Type>::_Assignable_requi
                        rement_violation(_Type) [with _Type=Request]" at line 8 of
                        "ComeauTest.c"
    Code:
    #include <list>
    struct Request;
    
    struct Requester
    {
    	bool beingServiced;
    	std::list<Request> requests;
    };
    
    struct Request
    {
    	Requester r;
    	int track;
    };
    MinGW and VC++ appear to compile it without errors, though.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  7. #7
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Comeau is just being aggressive.

    The standard says that a template type argument may be an incomplete type. (14.3.1.2)

    Soma

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    The standard also says that using an incomplete type as the type of a container is undefined behavior. (17.4.3.6p2)
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    It does. The rationale is ridiculous. Off hand, I don't know of any implementation where it doesn't work. (This goes for the version of Comeau I have installed. The definition of the containers under certain assumptions are setup to instantiate firewalls which naturally fail even before any functions are invoked.)

    By the by, I do not intend to imply that I think the design here is good.

    Soma

  10. #10
    Registered User
    Join Date
    Jul 2007
    Posts
    186
    It probably isn't. I'm new to C++ and for a project I'm doing I have a list of Requestors each having their own Requests. The Requests have to know where they came from. This seemed the easiest way to do it.

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by jcafaro10 View Post
    It probably isn't. I'm new to C++ and for a project I'm doing I have a list of Requestors each having their own Requests. The Requests have to know where they came from. This seemed the easiest way to do it.
    Then clearly, you need a pointer to the requestor that it came from, not a copy of the requestor.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    With that in mind, the 'Request' object doesn't own its "requesting" object--the 'Requester' class. The 'Request' object only needs a link to the "requesting" object. You want a pointer, or for a preference, given the original post, a weak pointer.

    Soma

  13. #13
    The larch
    Join Date
    May 2006
    Posts
    3,573
    But the requests don't know where they are coming from, do they? Instead they create a totally new and unrelated Requester instance...

    If the contained object need to know about its owner, you do need a pointer (or reference) to it. And then you should be able to forward declare it the other way round...
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 04:35 PM
  2. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  3. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  4. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM