简体   繁体   中英

Declaration of inner struct in another struct results in error: invalid use of struct

This is my first question here. I started learning C++ this summer and I have a little problem with this part of the code:

struct Contact{
    char name[30];
    char surname[50];
    int age;
    struct Born{
        int day;
        int month;
        int year;
    };
    char mail[50];
};

Well, I think this part of the code is OK, but I have this function to enter information and I don't know what I'm doing wrong.

Contact readContact(){
    Contact c;
    cout << "\t            NAME: ";
    cin.getline(c.name,30);
    cout << "\t         SURNAME: ";
    cin.getline(c.surname,50);
    cout << "\t             AGE: ";
    cin >> c.age;
    cout << "\t       BIRTHDATE:";
    cin >> c.Born.day >> c.Born.month >> c.Born.year;
    cout << "\t           EMAIL: ";
    cin.getline(c.mail,50);
    cin.ignore();
    return c;
}

I get following error message:

invalid use of 'struct Contact::Born'|

Maybe it's a simple error, but I'm new at coding, and I can't see what I'm doing wrong. :(

As mentioned in a comment, the symbol Born is a type , much like Contact is.

The simple solution is to declare the structure as an anonymous type for a variable:

struct Contact{
    char name[30];
    char surname[50];
    int age;
    struct /* No type name here */ {
        int day;
        int month;
        int year;
        } Born;  // Member variable name here
    char mail[50];
};

You can define the inner struct as anonymous type:

struct Contact
{
   char name[30];
   char surname[50];
   int  age;
   struct /* anonymous */
   {
      int day;
      int month;
      int year;
   } Born; /* member name */
   char mail[50];
};

This would mean that you have no explicit type for the inner struct (it's anonymous) so that you can not declare variables of that inner anonymous type in your other code.

To allow usage of the inner struct you can separate them:

struct Born_s
{
   int day;
   int month;
   int year;
};

struct Contact
{
   char   name[30];
   char   surname[50];
   int    age;
   Born_s Born;
   char   mail[50];
};

or give it a type name inside the structure (usage of the inner type: Contact::Born_s ) like:

struct Contact
{
   char name[30];
   char surname[50];
   int  age;
   struct Born_s /* inner type name: Contact::Born_s */
   {
      int day;
      int month;
      int year;
   } Born; /* member name */
   char mail[50];
};

As others have pointed out Born as you have declared it, is a type not a member. But you can use it to declare a member like so:

struct Contact{
    char name[30];
    char surname[50];
    int age;
    struct Born{
        int day;
        int month;
        int year;
        };
    Born birthDate; //<- member declaration using Contact::Born as a type
    char mail[50];
};

This is useful since you can later use the type eg as a function parameter:

struct Contact {
    // ... member decleration
    void setBirthDate(Born newDate) {
        birthDate = newDate;
    }
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM