Thread: Problem declaring instance of enclosing class within a nested struct

  1. #1
    Registered User Sir Galahad's Avatar
    Join Date
    Nov 2016
    Location
    The Round Table
    Posts
    277

    Problem declaring instance of enclosing class within a nested struct

    Basically have something like this:

    Code:
    struct enclosing
    {
       struct nested
       {
          enclosing instance;      
       };   
    };
    For some reason the compiler complains that field 'instance' has incomplete type of {anonymous}::enclosing. I really don't want to have to make a forward declaration, pulling the struct outside of the class. Is there a nicer workaround for that?
    Last edited by Sir Galahad; 09-03-2018 at 04:53 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Why do you want the nested struct to have an instance of the enclosing struct type in the first place? It is far more typical to expect that the enclosing struct would be the one to have an instance of the nested struct, or the nested struct might refer to the enclosing struct through a (smart) pointer. A forward declaration wouldn't cut it either since you need the struct definition, not a mere forward declaration, to declare an instance of it. Perhaps you would like to move the nested struct into a namespace instead?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User Sir Galahad's Avatar
    Join Date
    Nov 2016
    Location
    The Round Table
    Posts
    277
    Quote Originally Posted by laserlight View Post
    Why do you want the nested struct to have an instance of the enclosing struct type in the first place? It is far more typical to expect that the enclosing struct would be the one to have an instance of the nested struct, or the nested struct might refer to the enclosing struct through a (smart) pointer. A forward declaration wouldn't cut it either since you need the struct definition, not a mere forward declaration, to declare an instance of it. Perhaps you would like to move the nested struct into a namespace instead?
    It's a little complicated but basically the enclosing class defines certain template functions, and I'm using the nested structs as various "adapters" used to invoke the template functions in order to obtain a given functionality.

    But now that I think of it, one thing I could probably do is just define the nested structs as templates which takes the enclosing class as a parameter. Kind of a hack but much nicer than having to pull it out of the class. I'll see if I can get that to work first, otherwise I'll just follow your advice and put it inside of a namespace...

  4. #4
    Registered User Sir Galahad's Avatar
    Join Date
    Nov 2016
    Location
    The Round Table
    Posts
    277
    Nope. The initial declaration compiles fine, but then when I actually invoke the enclosing class' template function (which treats the nested struct instance as a function object) I basically get the same error. I even tried making the the nested struct's functor operator a template itself but that didn't help any.

  5. #5
    Registered User Sir Galahad's Avatar
    Join Date
    Nov 2016
    Location
    The Round Table
    Posts
    277
    Okay on second thought this might work after all. I put together a simplified version of what I'm trying to do and it compiled fine.

    Code:
    struct enclosing 
    {     
        struct A 
        { 
            A(int val) 
            : var(val) 
            { 
                // blah blah blah 
            } 
             
            void operator()(enclosing& ref) 
            { 
                // do stuff here with "ref" using "var" 
            } 
             
            int var;         
        }; 
         
        template <typename E> 
        struct B 
        { 
            B(const E& val) 
            : dup(val) 
            { 
                // blah blah blah 
            } 
             
            void operator()(E& ref) 
            { 
                // do stuff here with "ref" using fields of "dup" 
            } 
             
            E dup;            
        };    
         
        template <typename G> 
        void work(G fun) 
        { 
            // do some things 
            fun(*this); 
            // do more stuff here 
        } 
         
        void work(int val) 
        { 
            work(A(val)); 
        } 
         
        void work() 
        { 
            work(B<enclosing>(*this)); 
        }     
    }; 
     
    int main() 
    { 
        enclosing e; 
        e.work(123); 
        e.work(); 
    }
    Now I just have to try to fix all the errors in my project to see if it will actually work in the wild.
    Last edited by Sir Galahad; 09-03-2018 at 06:53 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Declaring a new instance of a class:
    By jocdrew21 in forum C++ Programming
    Replies: 9
    Last Post: 11-07-2014, 04:44 PM
  2. Declaring struct problem
    By _DrMario_ in forum C Programming
    Replies: 2
    Last Post: 12-04-2006, 10:02 AM
  3. Declaring an instance of a class inside a class
    By nickodonnell in forum C++ Programming
    Replies: 4
    Last Post: 10-01-2005, 11:46 PM
  4. Replies: 4
    Last Post: 12-12-2002, 02:32 PM
  5. Problem declaring struct in allegro
    By bobish in forum Game Programming
    Replies: 7
    Last Post: 03-08-2002, 09:26 AM

Tags for this Thread