头文件中的结构声明问题:"错误:字段' stat'具有简化的类型 [英] Problem with declaration of structs in header files: "error: field 'stat' has incimplete type"

查看:64
本文介绍了头文件中的结构声明问题:"错误:字段' stat'具有简化的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

sched.h

   
   #ifndef __SCHED_H__
   #define __SCHED_H__

   #include <stats.h>

   struct stats;
   struct task_struct {
       struct stats stat;
   };

   #endif

文件统计信息.h

   #ifndef STATS_H
   #define STATS_H

   #include <sched.h>

   struct stats
   {
      int i;
   };
   #endif

我收到此错误:

 In file included from include/stats.h:6,
                 from stats.c:1:
include/sched.h:30:16: error: field ‘stat’ has incomplete type
  30 |   struct stats stat;
     |                ^~~~

很明显,sched.h文件在使用它之前没有看到stats.h文件的声明,但是我不知道如何解决此问题.

Clearly the sched.h file is not seeing the declaration of the stats.h file before using it, but i dont have any idea on how this can be fixed.

问题是,stats.h文件中没有太多代码,而sched.h文件很长,但是问题仅在于该声明.我无法在虚拟程序中重现该问题,但是在我添加此程序之前,一切工作正常.如果我删除了#include< sched.h>一切正常.

The thing is, there is not much more code in the stats.h file, and the sched.h file is quite long, but the problem is just with that declaration. I have not been able to replicate the problem in a dummy program tho, but everything was working fine till I added this. If I remove the #include <sched.h> everything works fine.

这些是我正在使用的编译命令.我只是在创建目标文件.

These are the commands I am using to compile. I am just creating the object files.

tats.o:stats.c $(INCLUDEDIR)/stats.h

sched.o:sched.c $(INCLUDEDIR)/sched.h  

推荐答案

您可以递归包含标头.

在标题 sched.h 中,您拥有

#include <stats.h>

另一方面,您在标题 stats.h 中拥有

On the other hand, in the header stats.h you have

#include <sched.h>

从您的代码片段显示的内容中,尚不清楚将标题 sched.h 包含在标题 stats.h includes中的原因是什么.

From presented by you code snippet it is unclear what is the reason of inclusion the header sched.h within the header stats.h includes .

在任何情况下,都应删除其中一个标头中的include指令之一.

In any case one of the include directives in one of the headers should be removed.

如果您在另一个结构定义(如

If you are using a data member of the type struct stats in another structure definition like

struct task_struct {
    struct stats stat;
};

然后应已定义结构 struct stats .那就是这种物体的尺寸应该是已知的.否则,它是不完整的类型.

then the structure struct stats shall be already defined. That is the size of an object of this type shall be known. Otherwise it is an incomplete type.

重新设计标题.

这篇关于头文件中的结构声明问题:&quot;错误:字段&amp;#39; stat&amp;#39;具有简化的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆