Thread: Nested Structures Possible?

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

    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
    28,413
    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;
    };
    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
    Join Date
    Aug 2005
    Posts
    204
    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
    28,413
    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.
    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

  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
    204
    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
    28,413
    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.
    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

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