Nested Structures Possible?

This is a discussion on Nested Structures Possible? within the C++ Programming forums, part of the General Programming Boards category; Is it possible to make nested structures? I'm trying to do this with the following code, but it just doesn't ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    197

    Nested Structures Possible?

    Is it possible to make nested structures? I'm trying to do this with the following code, but it just doesn't compile

    Code:
    #include<iostream>
    using namespace std;
    
    
    struct animals{
    	struct insects;
    	struct fish;
    	struct mammals;
    };
    
    struct insects{
    	char moths[40];
    	char beatles[40];
    	char crickets[40];
    };
    
    
    int main(){
    	
    	animals A;
    	A.insects B;
    	
    	return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,910
    Moved to the C++ programming board.

    For one thing, the structs fish and mammals do not exist. The next problem is that you did not provide any names for your member variables. It is as if you wrote:
    Code:
    struct Example {
        int;
        char;
        double;
    };
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    197
    The code below produces the following compilation error:
    nestedStruct.cpp:30: error: expected primary-expression before '.' token
    nestedStruct.cpp:30: error: expected ';' before "B"
    My actual purpose in this is to generate a "tree data structure."

    Code:
    #include<iostream>
    using namespace std;
    
    
    struct animals{
    	struct insects;
    	struct fish;
    	struct mammals;
    };
    
    struct insects{
    	char moths[40];
    	char beatles[40];
    	char crickets[40];
    };
    
    struct fish{
    	char bass[40];
    	char shark[40];
    };
    
    struct mammals{
    	char gopher[40];
    	char elephant[40];
    };
    
    int main(){
    	
    	animals A;
    	animals.insects B;
    	
    	return 0;
    }

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,910
    My actual purpose in this is to generate a "tree data structure."
    What do you mean? It looks as if you want to use inheritance.

    The closest that I can see to correcting your errors is:
    Code:
    #include<iostream>
    using namespace std;
    
    struct Insects {
        char moths[40];
        char beatles[40];
        char crickets[40];
    };
    
    struct Fish {
        char bass[40];
        char shark[40];
    };
    
    struct Mammals {
        char gopher[40];
        char elephant[40];
    };
    
    struct Animals {
        Insects insects;
        Fish    fish;
        Mammals mammals;
    };
    
    int main() {
        Animals a;
        // a.insects is an Insect struct.
    }
    But this is just composition: an Animals object has a Insects object.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code:
    struct animals{
    	struct insects;
    	struct fish;
    	struct mammals;
    };
    It looks like animals contains only forward declarations of other classes.

    You should move it after you have all those structs declared and then declare actual instances of those as members of struct animals.
    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).

  6. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    197
    Quote Originally Posted by laserlight View Post
    What do you mean? It looks as if you want to use inheritance.

    The closest that I can see to correcting your errors is:
    Code:
    #include<iostream>
    using namespace std;
    
    struct Insects {
        char moths[40];
        char beatles[40];
        char crickets[40];
    };
    
    struct Fish {
        char bass[40];
        char shark[40];
    };
    
    struct Mammals {
        char gopher[40];
        char elephant[40];
    };
    
    struct Animals {
        Insects insects;
        Fish    fish;
        Mammals mammals;
    };
    
    int main() {
        Animals a;
        // a.insects is an Insect struct.
    }
    But this is just composition: an Animals object has a Insects object.
    What I really want to do is dynamically generate a tree structure.

    What I can't seem to do in the above code is assign a value to the moths variable.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,910
    What I really want to do is dynamically generate a tree structure.
    The way you are doing it means that the tree structure is fixed at compile time. You would need to implement a multiway tree if you actually want a tree structure that can be changed at runtime.

    What I can't seem to do in the above code is assign a value to the moths variable.
    Of course you can. Just access a.insects.moths.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Nested Structures
    By leonv in forum C Programming
    Replies: 2
    Last Post: 01-03-2007, 02:52 PM
  2. Nested Structures - User Input
    By shazg2000 in forum C Programming
    Replies: 2
    Last Post: 01-09-2005, 09:53 AM
  3. Nested Structures
    By Thantos in forum C Programming
    Replies: 2
    Last Post: 12-07-2003, 02:34 PM
  4. Nested structures
    By Supar in forum C++ Programming
    Replies: 2
    Last Post: 04-03-2003, 08:27 PM
  5. Nested structures
    By Garfield in forum C Programming
    Replies: 8
    Last Post: 10-08-2001, 12:11 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21