ç未知的类型名称'my_structure“ [英] C Unknown type name 'my_structure'

查看:155
本文介绍了ç未知的类型名称'my_structure“的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的code:

main.h

#ifndef MAINH
#define MAINH
...
#include "my_struct.h"
void some_func(my_structure *x);
...
#endif

my_struct.h

#ifndef UTILSH
#define UTILSH
...
#include "main.h"
...
typedef struct abcd {
    int a;
} my_structure;
...
#endif

但我得到这个当我尝试编译:错误:未知类型名称'my_structure

任何想法,为什么?

推荐答案

无效some_func(my_structure * X); 它看到前 typedef结构ABCD {int类型的; } my_structure;

我想。

让我们通过走这条。

假设 my_struct.h 首先处理,我们得到的事件顺序如下:

Assuming my_struct.h is processed first, we get the following sequence of events:


  1. UTILSH 定义

  2. MAINH 定义

  3. 因为 UTILSH 已经定义,我们不这样做的过程 my_struct.h 了,所以typedef的ISN ŧ处理

  4. 无效some_func(my_structure * X); 处理

  5. 现在的typedef的处理。

  1. UTILSH is defined
  2. MAINH is defined
  3. Because UTILSH is already defined, we don't process my_struct.h again, so the typedef isn't processed
  4. void some_func(my_structure *x); is processed.
  5. Now the typedef is processed.

所以经过preprocessing,你的编译器看到声明的顺序如下:

So after preprocessing, your compiler sees the following sequence of declarations:

...
void some_func(my_structure *x);
...
typedef struct abcd {...} my_structure;

坏朱朱。您可能需要 my_structure main.h 预先声明,或者您需要打破这种循环依赖(这是备受preferred选项)。是否有 main.h my_structure.h 实际使用的东西吗?如果是这样,你将要因素出来成为一个单独的文件,这两个 main.h my_structure.h 包括。

Bad juju. You either need a forward declaration of my_structure in main.h, or you need to break that circular dependency (which is the much preferred option). Is there anything in main.h that my_structure.h actually uses? If so, you will want to factor it out into a separate file that both main.h and my_structure.h include.

这篇关于ç未知的类型名称'my_structure“的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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