CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Sep 2006
    Posts
    10

    Class containing struct referencing class

    For this problem, it might be simpler to begin with a code extract:

    Code:
    class Pattern
    {
    public:
       struct ChildPattern
       {
           Pattern pattern; 
           int offset;
       };
    };
    Instances of "ChildPattern" are going to be kept in lists and the like, and "offset" is something associated with instances of Pattern but it is not intrinsic to them (i.e. it shouldn't be part of the class, except as a hack).

    Anyway the problem is that the above code doesn't compile. I get
    Pattern::ChildPattern:: pattern uses undefined class 'Pattern'
    .
    Any suggestions? I don't want to change the first struct element to "Pattern * pPattern" if I can help it: the class is simple and I don't want to add new() and delete() unnecessarily.

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Class containing struct referencing class

    If this is in a header, you could write:
    Code:
    class Pattern
    {
    public:
    
    };
    
    namespace detail
    {
       struct ChildPattern
       {
           Pattern pattern; 
           int offset;
       };
    }
    with the convention that whatever is within a namespace named detail is implementation detail and hence private by convention.

    EDIT:
    Oh wait, I noticed that the nested class is public. Therefore, it would suffice to make it non-nested without the detail namespace (i.e., keeping it in the same namespace as Pattern will do).
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Sep 2006
    Posts
    10

    Re: Class containing struct referencing class

    Thanks. Still having trouble though.

    I should have added that the class needs to use this structure too (shown below), so evidently to the compiler, it is a circular definition.
    The thing is though, to my brain, it isn't a circular definition (), and there must be a way to push this through. I can only think of hack ways at the mo.

    Code:
    class Pattern
    {
    public:
       struct ChildPattern
       {
           Pattern pattern; 
           int offset;
       };
    
       void AddChild (ChildPattern & child);
    };

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Class containing struct referencing class

    You can use a forward declaration, e.g.,
    Code:
    struct ChildPattern;
    
    class Pattern
    {
    public:
        void AddChild(ChildPattern& child);
    };
    
    struct ChildPattern
    {
        Pattern pattern;
        int offset;
    };
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  5. #5
    Join Date
    Sep 2006
    Posts
    10

    Thumbs up Re: Class containing struct referencing class

    That sorted it.
    I had tried forward declaring the class, but for whatever reason I had a mental blindspot to the idea of forward declaring the struct.

    Thanks laserlight, I've just rated your post

  6. #6
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: Class containing struct referencing class

    Wait a minute...

    It's fine if you two got it working, but the OP's original code snippet was a class that (more or less indirectly) contained a member of its own type, which, for obvious reasons after some second thought, is never, never, never allowed in C++.

    Or did I miss some obscure loophole here?
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  7. #7
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Class containing struct referencing class

    Quote Originally Posted by Eri523 View Post
    Wait a minute...

    It's fine if you two got it working, but the OP's original code snippet was a class that (more or less indirectly) contained a member of its own type, which, for obvious reasons after some second thought, is never, never, never allowed in C++.

    Or did I miss some obscure loophole here?
    No, the (base) class defines a subclass that contains an instance of said base class. However, the actual base class does not contain an instance of the derived class, and as such, does not contain an instance of itself. However, you are perfectly correct about the fact that it is not legal (or even possible) to have a member of yourself.

    The OP's code did not fail because of this. THIS code though does...

    Code:
    class Pattern
    {
    public:
       struct ChildPattern
       {
           Pattern pattern; 
           int offset;
       } an_instance;
    
       ChildPattern another_instance;
    };
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  8. #8
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Class containing struct referencing class

    Quote Originally Posted by monarch_dodra
    No, the (base) class defines a subclass that contains an instance of said base class. However, the actual base class does not contain an instance of the derived class, and as such, does not contain an instance of itself.
    I would rather say that the enclosing class definition contains a nested class definition that has an enclosing class object as a member. Base class, subclass and derived class usually imply that inheritance is involved, but that is not the case here.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  9. #9
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: Class containing struct referencing class

    Quote Originally Posted by monarch_dodra View Post
    However, the actual base class does not contain an instance of the derived class, and as such, does not contain an instance of itself.
    Ah, I see. Thanks, now I'm content again.

    Am I right to assume that in this case the visibility of ChildPattern is limited to the scope of the Pattern declaration? I.e. the member function from post #3 could only be called from inside another member function?
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  10. #10
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Class containing struct referencing class

    Quote Originally Posted by Mijin View Post
    Code:
    class Pattern
    {
    public:
       struct ChildPattern
       {
           Pattern pattern; 
           int offset;
       };
    
       void AddChild (ChildPattern & child);
    };
    Quote Originally Posted by Eri523 View Post
    Am I right to assume that in this case the visibility of ChildPattern is limited to the scope of the Pattern declaration? I.e. the member function from post #3 could only be called from inside another member function?
    Am not sure I completely understand your question (especially since post #3 doesn't compile) but my answer would be no. Nesting a declaration merely ads scope to the declarations (like namespaces), and since everything here is public, this would be perfectly legal:

    Code:
    Pattern myPattern;
    Pattern::ChildPattern myChild;
    
    myPattern.AddChild(myChild);
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  11. #11
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: Class containing struct referencing class

    Thanks, monarch_dodra. You understood my question perfectly and your answer was exactly what I wanted to know.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

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