Home > Software design >  Why does my code say that it has an incomplete type even after I tried declaring it?
Why does my code say that it has an incomplete type even after I tried declaring it?

Time:11-15

I am writing a program that displays a number of students test scores in the form of a table and then calculates and displays the average. Upon running the code, I get the following errors(also pictured below): variable has incomplete type 'struct students' struct students st[50]; ^ note: forward declaration of 'students' struct students st[50];

I have declared students and have tried declaring st and I am just not sure what the problem is. Below is a typed version and a screenshot of my code:

#include <iostream>
using namespace std;
int Main()
{
  int students;

   struct students st[50]; 
   return 0;
}

Code errors Picture of typed code

CodePudding user response:

The structure students used in this declaration

struct students st[50];

is not defined yet. So the compiler issues an error because you may not declare an array with an incomplete element type.

You should define the structure before using its elaborated name in the array declaration as for example

include <string>

struct students
{
    std::string name;
};

int main()
{
    int students;

    struct students st[50];

    //...
}

CodePudding user response:

You have to define it first. You can also use this style.

struct student{
    ...
};

int main{
    student* st[50];
    for(int i=0; i<50;i  )
        st[i]=new student;
    return 0;
}
  • Related