头文件原型中的未知类型名称

时间:2013-11-09 13:30:18

标签: c typedef header-files function-prototypes

我有 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可以看到字符串。 但我在头文件中的原型上有编译错误:

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

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

1 个答案:

答案 0 :(得分:2)

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

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

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

由于lexer.h 始终需要包含str.h,因此最好将#include "str.h"输出到此标头文件中。

相关问题