【问题标题】:Unknown type name in header file prototype头文件原型中的未知类型名称
【发布时间】:2013-11-09 13:30:17
【问题描述】:

我有 lexer.c 文件,它应该包含在另一个 .c 文件中。它有

int getToken(string *attribute) {}

lexer.h 头文件中的

函数和原型相同。我还有帮助 str.c 文件来简化字符串的工作。它有带有 string 类型声明的头文件:

typedef struct {
    char* str;      //string with \0 at the end
    int length;     //length of the string
    int allocated;  //allocated memory size
} string;

所以,lexer.h 包含在主文件中。然后 lexer.c 开始于:

#include "str.h"
#include "lexer.h"

据我了解,在包含 str.h 后,lexer.c 和 lexer.h 可以看到类型 string。 但是我在头文件中的原型有编译错误:

./lexer.h:65:14: error: unknown type name 'string'
int getToken(string *attribute);
             ^
1 error generated.

如何在头文件中使用这种类型?

【问题讨论】:

  • 检查 #ifdef 或其他原因,您的 typedef 可能会被排除在外...与您的问题无关,但 I have lexer.c file, which should be included to another .c file. - 如果那不是类型...。不要 #include one 。 c 变成另一个。有时它可能工作正常,但作为一种模式遵循它最终会导致问题。
  • 你为什么不把它简单化,你的自定义声明到一个通用的头文件中,并将它包含在所需的 .c 文件中。
  • @mah 对不起我的写作风格,我的意思是通过 lexer.h 头文件包含到另一个 .c 文件中

标签: c typedef header-files function-prototypes


【解决方案1】:

我现在不清楚哪些文件包含哪些。让我试着概括一下:

  • lexer.c 包括 str.hlexer.h
  • main.c 包括 lexer.h

是这样吗?在这种情况下,main.c 无法编译,因为确实缺少 string 类型的定义。

由于lexer.h 总是 需要包含str.h,因此将#include "str.h" 输出到此头文件中可能是个好主意。

【讨论】:

  • 是的,没错!感谢您的帮助,包括 str.h 到 main.c 解决了我的问题。在头文件中没有#include“str.h”的情况下工作
  • @MarkBirger 当然可以。但是,如果所有头文件都是“独立的”,它可能有助于防止此类事情发生,即。 e.工作时不需要包含器也包含所需的东西。
  • 哦..我明白了,如果我将所有“依赖项”包含到 .h 文件中,我不需要将 str.h 包含到每个文件中。
  • 是的。至少,前提是所有.h 文件都支持多次包含,例如。 G。具有包含保护保护,或者足够简单以包含可能重复的语句。
猜你喜欢
  • 2021-07-21
  • 2023-04-11
  • 1970-01-01
  • 2013-04-26
  • 1970-01-01
  • 2014-11-19
  • 2018-10-27
  • 2016-09-16
相关资源
最近更新 更多