在彼此中使用struct和enums的标头?

时间:2017-03-06 15:05:04

标签: c struct enums header-files

所以我将我的代码转换为使用头文件的新结构(我应该注意,我不经常使用它们)并且我无法将其编译(使用gcc) --std = C11)。我想我知道问题是什么,我只是不知道如何实现代码来修复它。

这里有一些描述问题的代码。

的main.c

#include "file1.h"
#include "file2.h"
#include "header.h"

int main( )
{
    BigStruct bigStruct;

    DoStuff( &bigStruct );
    OtherStuff( &bigStruct );

    return 0;
}

file1.h

#ifndef FILE1_H
#define FILE1_H

#include "header.h"

typedef enum TagMyEnum
{
    MY_THING1,
    MY_THING2,

    MY_ENUM_END
}
MyEnum;

typedef struct TagMyStruct
{
    // Variables.
}
MyStruct;

typedef struct TagBigStruct BigStruct;

void DoStuff( BigStruct *bigStruct );

#endif

file2.h

#ifndef FILE2_H
#define FILE2_H

#include "header.h"

typedef enum TagOtherEnum
{
    OTHER_THING1,
    OTHER_THING2,

    OTHER_ENUM_END
}
OtherEnum;

typedef struct TagOtherStruct
{
    // Variables.
}
OtherStruct;

typedef struct TagBigStruct BigStruct;

void OtherStuff( BigStruct *bigStruct );

#endif

header.h

#ifndef HEADER_H
#define HEADER_H

#include "file1.h"
#include "file2.h"

typedef struct TagMyStruct MyStruct;
typedef struct TagOtherStruct OtherStruct;

typedef struct TagBigStruct
{
    MyStruct myStruct[MY_ENUM_END];
    OtherStruct otherStruct[OTHER_ENUM_END];
}
BigStruct;

#endif

编译器告诉我这个。

In file included from file1.h:4:0,
                 from main.c:2:
header.h:12:23: error: ‘MY_ENUM_END’ undeclared here (not in a function)
     MyStruct myStruct[MY_ENUM_END];

现在我认为我理解错误,我只是不知道如何实施解决方案。

1 个答案:

答案 0 :(得分:1)

Header.h需要MY_ENUM_END,但在MyEnum

file1.h的定义之前已包含#ifndef FILE1_H #define FILE1_H typedef enum TagMyEnum { MY_THING1, MY_THING2, MY_ENUM_END } MyEnum; #include "header.h" // ...

这是调用循环依赖

轻松修复

android:includeFontPadding="false"

file2.h也是如此。

最好重新设计标题,以避免它们之间的交叉引用。