简体   繁体   English

C 错误中的外部结构:数组类型具有不完整的元素类型

[英]extern structs in C Error: array type has incomplete element type

I have two files, main.c and other.h .我有两个文件, main.cother.h I declare a few global variables in main.c , and most of them are fine.我在main.c声明了几个全局变量,大部分都没有问题。 I can say extern whatever in other.h and access those variables.我可以在other.hextern whatever other.h并访问这些变量。 However, I defined a struct like this (in main.c ):但是,我定义了一个这样的结构(在main.c ):

struct LSA {
     int myID;
     int seqNum;
     int neighborID[256];
     int costs[256];
 } local_LSA[256];

And when I try to access it in other.h like this当我尝试像这样在other.h访问它时

extern struct LSA local_LSA[256];

I get this error message我收到此错误消息

other.h:27:19: error: array type has incomplete element type ‘struct LSA’
extern struct LSA local_LSA[256];

I've been playing around with for a while, but... I appreciate any help anyone is able to provide!我已经玩了一段时间了,但是......我感谢任何人能够提供的任何帮助!

The structure type needs to be defined before any instances of it can be created.需要先定义结构类型,然后才能创建它的任何实例。

You need to move the struct definition into other.h before local_LSA is defined.您需要结构定义移动到other.h之前local_LSA定义。

The header file isn't aware of the struct definition of struct LSA .头文件不知道struct LSA的结构定义。 What you should do instead is to declare the struct in some "lsa.h" then define and allocate the variables of that type in some .c file.您应该做的是在某些“lsa.h”中声明结构,然后在某些 .c 文件中定义和分配该类型的变量。

Please note that the design you suggest is spaghetti programming.请注意,您建议的设计是意大利面条式编程。 There should never be a reason for another file to extern access some variable in main.c, which is the top level of your program - or otherwise your program design is flawed.另一个文件永远不应该有理由extern访问 main.c 中的某些变量,这是您程序的顶级 - 否则您的程序设计有缺陷。 Use setter/getter functions instead of spaghetti.使用 setter/getter 函数代替意大利面条。

According to the C Standard (6.7.6.2 Array declarators)根据 C 标准(6.7.6.2 数组声明符)

  1. ... The element type shall not be an incomplete or function type . ...元素类型不应是不完整或函数类型

In this declaration of an array within the file other.h在文件other.h的这个数组声明中

extern struct LSA local_LSA[256];

the element type struct LSA is incomplete type because the structure definition at this point is unknown.元素类型struct LSA是不完整类型,因为此时的结构定义是未知的。

So the compiler issues the corresponding error message.所以编译器会发出相应的错误信息。

other.h:27:19: error: array type has incomplete element type ‘struct LSA’
extern struct LSA local_LSA[256];

What you need is to place the structure definition in the header before the array declaration.您需要的是将结构定义放在数组声明之前的标头中。

You could write for example in the header file other.h例如,您可以在头文件other.h

struct LSA {
     int myID;
     int seqNum;
     int neighborID[256];
     int costs[256];
 };
 
 extern struct LSA local_LSA[256];

Pay attention to that this record注意这个记录

 extern struct LSA local_LSA[256];

is a declaration of an array not a definition.是数组的声明而不是定义。

Then in main.c you could place one more declaration of the array like然后在main.c您可以再放置一个数组声明,例如

struct LSA local_LSA[256];

in this case the declaration of the array will generate the so-called tentative definition of the array.在这种情况下,数组的声明将生成所谓的数组暂定定义。

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

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