You are here:

C++/Nested Structures C++

Advertisement


Question
I am trying to write this code using nested structures when I run my program however I get a request error. Here is my code.

#include<iostream>
using namespace std;
// program Objective: To write a nested structure.  

struct Mystruct // nested structure where Mystruct1 is embedded in the first structure Mystruct.
{
int x;
int y;

};

struct Mystruct1
 {
  int x1;
  int y1;
  int z1;
  }; // no semicolon needed here since it is a nested structure.


int main()
{


Mystruct S; // create a instance.

S.x.x1 = 5; // a error on this line maybe syntax problem?
cout << "The first combined element is " << S.x.x1 << endl;


Answer
Well reading the _code_ posted in your question and not the comments shows no nesting of struct definitions nor of struct instance members between Mystruct and Mystruct1 - both struct definitions appear to be within the same namespace - the global namespace - and independent of each other.

These are the definitions I received in your question:

   struct Mystruct
   {
       int x;
       int y;
   };

   struct Mystruct1
   {
       int x1;
       int y1;
       int z1;
   };

Is this really what you intended to post in your question?

If not then please post a followup showing the code you intended to post, if my comments below do not cover what you are having problems with.

Before getting into nesting of class types (classes, structs and unions) I would like to say something about the second comment in the posted code:

   // no semicolon needed here since it is a nested structure.

This is _not_ true in C and C++. In C and C++ struct, union (and class for C++) definitions are _always_ terminated with a semicolon.They may optionally be followed by object or data member names:

   struct Point
   {
       int x;
       int y;
   } a_point, another_point;

Which is not a very common style in modern C++, but probably explains the reason behind the requirement for a semicolon - note that the newer C++ feature of namespaces do not require a semicolon when they are closed:

   namespace MyNamingspace
   {

       ...

   }

Probably because you cannot have named instances (i.e. objects) of namespaces.

Now onto nesting of types.

We can nest a struct class or union definition within another class struct or union definition like so:

   struct Mystruct
   {
       struct Mystruct1
       {
           int x1;
           int y1;
           int z1;
       };      // Semicolon required even for nested type definitions
               // (try removing it and see what the compiler has to say about it!)

       int x;
       int y;
   };

However this does _not_ mean that each instance of MyStruct contains an instance of the inner Mystruct1 struct. All it means is that the definition of Mystruct1 is within the namespace of the outer Mystruct definition and so has to be referred to using a qualified name outside of Mystruct:

   int main()
   {
       Mystruct S; // S contains an x and a y member, both of type int
       S.x = 1;
       S.y = 2;

       Mystruct::Mystruct1 S1; // S1 contains an x1, a y1 and a z1 member, all of type int
       S1.x1 = 10;
       S1.y1 = 20;
       S1.z1 = 30;
   }

To get actual instances of type Mystruct::Mystruct1 as members of Mystruct instances we have to explicitly define data members for this type:

   struct Mystruct
   {
       struct Mystruct1
       {
           int x1;
           int y1;
           int z1;
       };

       Mystruct1 s;
       int x;
       int y;
   };

Now we have a MyStruct definition which contains a Mystruct::Mystruct1 member called s, so now we can write:

   int main()
   {
       Mystruct S; // S now contains an s member of type Mystruct::Mystruct1
                   // and an x and a y member, both of type int
       S.x = 1;
       S.y = 2;

       S.s.x1 = 10;
       S.s.y1 = 20;
       S.s.z1 = 30;
   }

Notice that within Mystruct we can refer to Mystruct::Mystruct1 unqualified as just Mystruct1 as it is within the namespace scope of Mystruct. We could also have defined s before the semicolon terminating the definition of Mystruct::Mystruct1:

   struct Mystruct
   {
       struct Mystruct1
       {
           int x1;
           int y1;
           int z1;
       } s;

       int x;
       int y;
   };

Hope this clarifies nesting of class types and using data members of a class type that are instances of other class types - whose definitions may or may not be nested within the type defining the data members.  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Ralph McArdell

Expertise

I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.

Experience

My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at http://www.open-std.org/jtc1/sc22/wg21/.

Education/Credentials

©2012 About.com, a part of The New York Times Company. All rights reserved.