简体   繁体   中英

Error “Incomplete type is not allowed” in struct

I am receiving "Incomplete type is not allowed" error in my c++ code.

My program is to read in student data from the input file, sort names in alphabetical order, and print out sorted names along with its data. The data includes ID numbers and test scores.

The input file includes the repetition of the following: 1. Number of students (which is 16) 2. Number of tests (which is 6) 3. Name of student 4. ID number 5 - 10. Test scores

The output file should include the repetition of the following: 1. Name 2. ID number 3. First test score 4. Average score 5. Letter grade

The following are my lines for struct:

struct sGrade 
{ 
    string Name[20]; 
    int IDnum[20]; 
    float scores[5]; 
}; 

But right here, in the mainline, "grade" from the "sGrade grade[size]" line receives that sort of error.

string s;
getline(inputf, s);
sizeS = stoi(s);

string t;
getline(inputf, t);
sizeT = stoi(t);

const int size = 16;
sGrade grade[size];
for (int i = 0; i < size; ++i)
{
    getline(inputf, grade[i].Name);
    getline(inputf, s);
    grade[i].IDnum = stoi(s);
    for (int j = 0; j < sizeT; ++j)
    {
        getline(inputf, grade[i].scores);
    }
}

What has been written incompletely? Do I have to approach it in another way? Please suggest me some solutions to this.

Thank you!

So there are many problems in the code, mostly around type incompatibilities.

getline(inputf, grade[i].Name);

means grade[i].Name should be a string but it's declared as an array of 20 strings.

grade[i].IDnum = stoi(s);

means grade[i].IDnum should be an int but it's declared as an array of 20 ints

getline(inputf, grade[i].scores)

means grade[i].scores should be a string but it's declared as an array of 5 floats.

You have to think more carefully about the types you declare and the names you give things. Do you really want sGrade to have 20 names and 20 IDs? Or (more likely) do you really want sGrade to have a single name and a single ID, but then declare an array of 20 sGrades?

Here's what I think the code should be, but only you'll know for sure

struct sGrade 
{ 
    string Name; 
    int IDnum; 
    float scores[5]; 
}; 

for (int i = 0; i < size; ++i)
{
    getline(inputf, grade[i].Name);
    getline(inputf, s);
    grade[i].IDnum = stoi(s);
    for (int j = 0; j < sizeT; ++j)
    {
        getline(inputf, s);
        grade[i].scores[j] = stof(s);
    }
}

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