What does "expected unqualified-id" mean?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Warrax
    New Member
    • Jun 2007
    • 3

    What does "expected unqualified-id" mean?

    I am currently doing online tutorials for C++, and am pretty much stuck in a rut about this problem. It is saying that there's an expected unqualifed-id before '{' token (I will post the code in just a second) on line 11, and an expected ',' or ';' before '{' token also on line 11, however I don't have a clue what the first one means. The program is meant to save a user's name in a string variable within a structure, and to then show that name without the need to assign a pointer to it. Also, note that I am compiling this with the Dev-C++ (aka Devcpp) compiler, on a Windows XP system.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        struct database {
               string name; // declares that the structure has a string
               // in it called name.
    }
    {
               int main()
    cout<<"Enter your name.\n"; //displays the obvious
    getline(cin, name, '\n'); // records the user's input into the string name,
    // and terminates the command when the user presses enter.
           database employee01;
           // declares the single structure and its contents
           employee01.name = string name;
    cout<<"Your name should be: "<<employee01.name<<".\n";
    cin.get();
    }
    }
    11 C:\Documents and Settings\Main\M y Documents\subst ring and structures.cpp expected unqualified-id before '{' token
    11 C:\Documents and Settings\Main\M y Documents\subst ring and structures.cpp expected `,' or `;' before '{' token

    Could someone please explain to me what the "unqualifie d-id before" means, how to fix it, what I did wrong, and why it is the way that you say is the correct code?
  • ilikepython
    Recognized Expert Contributor
    • Feb 2007
    • 844

    #2
    Originally posted by Warrax
    I am currently doing online tutorials for C++, and am pretty much stuck in a rut about this problem. It is saying that there's an expected unqualifed-id before '{' token (I will post the code in just a second) on line 11, and an expected ',' or ';' before '{' token also on line 11, however I don't have a clue what the first one means. The program is meant to save a user's name in a string variable within a structure, and to then show that name without the need to assign a pointer to it. Also, note that I am compiling this with the Dev-C++ (aka Devcpp) compiler, on a Windows XP system.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        struct database {
               string name; // declares that the structure has a string
                                  // in it called name.
    }
    {
               int main()
    cout<<"Enter your name.\n"; //displays the obvious
    getline(cin, name, '\n'); // records the user's input into the string name,
    // and terminates the command when the user presses enter.
           database employee01;
           // declares the single structure and its contents
           employee01.name = string name;
    cout<<"Your name should be: "<<employee01.name<<".\n";
    cin.get();
    }
    }
    11 C:\Documents and Settings\Main\M y Documents\subst ring and structures.cpp expected unqualified-id before '{' token
    11 C:\Documents and Settings\Main\M y Documents\subst ring and structures.cpp expected `,' or `;' before '{' token

    Could someone please explain to me what the "unqualifie d-id before" means, how to fix it, what I did wrong, and why it is the way that you say is the correct code?
    Ok first let me post your code:
    [code=cpp]
    #include <iostream>

    using namespace std;

    int main()
    {
    struct database {
    string name; // declares that the structure has a string
    // in it called name.
    }
    {
    int main()
    cout<<"Enter your name.\n"; //displays the obvious
    getline(cin, name, '\n'); // records the user's input into the string name,
    // and terminates the command when the user presses enter.
    database employee01;
    // declares the single structure and its contents
    employee01.name = string name;
    cout<<"Your name should be: "<<employee01.n ame<<".\n";
    cin.get();
    }
    }
    [/code]
    There are a couple of errors:
    1. You have two mains
    2. Structs should be declared outside of main

    I think you need something like this:
    [code=cpp]
    struct database
    {
    string name;
    };

    int main()
    {
    database employee01;
    string name;
    cout << "Enter your name: ";
    getline(cin, name);
    employee01.name = name;
    cout << "Your name is: " << employee01.name << "." << endl;
    return 0;
    }
    [/code]

    Comment

    • Warrax
      New Member
      • Jun 2007
      • 3

      #3
      When I put the struct before main and get rid of the 2nd main, this appears:

      14 C:\Documents and Settings\Main\M y Documents\subst ring and structures.cpp expected primary-expression before "name"
      14 C:\Documents and Settings\Main\M y Documents\subst ring and structures.cpp expected `;' before "name"
      14 C:\Documents and Settings\Main\M y Documents\subst ring and structures.cpp At global scope:
      17 C:\Documents and Settings\Main\M y Documents\subst ring and structures.cpp expected unqualified-id at end of input
      17 C:\Documents and Settings\Main\M y Documents\subst ring and structures.cpp expected `,' or `;' at end of input

      Any ideas?

      Comment

      • ilikepython
        Recognized Expert Contributor
        • Feb 2007
        • 844

        #4
        Originally posted by Warrax
        When I put the struct before main and get rid of the 2nd main, this appears:

        14 C:\Documents and Settings\Main\M y Documents\subst ring and structures.cpp expected primary-expression before "name"
        14 C:\Documents and Settings\Main\M y Documents\subst ring and structures.cpp expected `;' before "name"
        14 C:\Documents and Settings\Main\M y Documents\subst ring and structures.cpp At global scope:
        17 C:\Documents and Settings\Main\M y Documents\subst ring and structures.cpp expected unqualified-id at end of input
        17 C:\Documents and Settings\Main\M y Documents\subst ring and structures.cpp expected `,' or `;' at end of input

        Any ideas?
        Could you please post the exact code that you used?

        Comment

        • Warrax
          New Member
          • Jun 2007
          • 3

          #5
          Originally posted by ilikepython
          Could you please post the exact code that you used?
          Sure.

          Code:
          #include <iostream>
          
          using namespace std;
          struct database {
                     string name; // declares that the structure has a string
                     // in it called name.
          int main()
          {
          cout<<"Enter your name.\n"; //displays the obvious
          getline(cin, name, '\n'); // records the user's input into the string name,
          // and terminates the command when the user presses enter.
                 database employee01;
                 // declares the single structure and its contents
                 employee01.name = string name;
          cout<<"Your name should be: "<<employee01.name<<".\n";
          cin.get();
          }
          }

          Comment

          • ilikepython
            Recognized Expert Contributor
            • Feb 2007
            • 844

            #6
            Originally posted by Warrax
            Sure.

            Code:
            #include <iostream>
            
            using namespace std;
            struct database {
                       string name; // declares that the structure has a string
                       // in it called name.
            int main()
            {
            cout<<"Enter your name.\n"; //displays the obvious
            getline(cin, name, '\n'); // records the user's input into the string name,
            // and terminates the command when the user presses enter.
                   database employee01;
                   // declares the single structure and its contents
                   employee01.name = string name;
            cout<<"Your name should be: "<<employee01.name<<".\n";
            cin.get();
            }
            }
            You never put the closing brace on the struct. Also, remember that you need a semicolon as well. Did you see the code I posted in my previous reply?

            Comment

            • turps
              New Member
              • Jun 2019
              • 2

              #7
              sorry, I have no idea - all replies to this question give alternative codes, but don't explain what unqualified-id means.

              my850+ page book on C++ does not mention either id or unqualified in its index.
              Last edited by zmbd; Jun 18 '19, 11:28 PM.

              Comment

              • zmbd
                Recognized Expert Moderator Expert
                • Mar 2012
                • 5501

                #8
                Turps asked this question again here and with a fairly good answer by Dev7060:

                Comment

                Working...