简体   繁体   English

为什么即使在我尝试声明它之后,我的代码仍然说它具有不完整的类型?

[英]Why does my code say that it has an incomplete type even after I tried declaring it?

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];运行代码后,我收到以下错误(也如下图所示):变量的类型不完整 'struct student' struct Students st[50]; ^ note: forward declaration of 'students' struct students st[50]; ^ 注意:'students' struct student st[50] 的前向声明;

I have declared students and have tried declaring st and I am just not sure what the problem is.我已经声明了学生并尝试声明了 st ,但我不确定问题是什么。 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代码错误键入代码的图片

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 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么尽管我声明了一个不完整类型的自动实例,但该代码仍会编译并运行 - why does this code compile and run despite me declaring an automatic instance of incomplete type QDate类型不完整,声明为私人成员 - QDate has incomplete type, declaring as private member 即使添加了前向引用,也会获取“字段的类型不完整” - Getting “field has incomplete type” even after adding forward reference 为什么我的代码应该说“否”时却说“是”? - Why does my code say “Yes” when it should say “No”? 为什么我的代码说109不是素数? - Why does my code say 109 is not a prime? 为什么此代码不起作用? 我收到一个错误:类型不完整或未命名类型 - Why doesn't this code work? I got an error: incomplete type or does not name a type 为什么错误不断返回“变量的类型不完整”? - Why does the error keep returning "variable has incomplete type void"? 为什么我的代码说 90 重复了 6 次,而不是 5 次? - Why does my code say that 90 was repeated 6 times, instead of 5? 我试图编译我的代码并得到错误`class TIME不命名类型`中的时间显示 - I tried to compile my code and got error `time display in class TIME does not name a type` 为什么这段代码说我需要一个类/结构/联合? - Why does this code say I need a class/struct/union?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM