c - 枚举和结构定义

标签 c header enums struct redefinition

使用代码块编译我的项目时出现错误。 我的问题来自枚举定义和结构定义。

它们是在头文件中定义的机器人,因为我只在 .c 关联文件中使用那些枚举和结构,所以它起作用了。但是当我将 .h 文件包含在另一个 .c 文件中时,出现错误,这是一些代码;

ma​​ps.h

#include <stdlib.h>
#include <stdio.h>

enum Property { BACKGROUND, FOREGROUND, BLOCK, EVENT };

typedef struct {
   char map_name[50];
   int width;
   int height;
   char* map_struct;
}rpgMap;
char getTileProperty(rpgMap map, int x, int y, int property);

ma​​ps.c

#include "maps.h"

char getTileProperty(rpgMap map, int x, int y, int property){    // Works
   char value = NULL;
   value = map.map_struct[(((y*(map.width-1))+y+x) * 4 ) + property];
   return value;
}   
rpgMap loadMap(unsigned char* map){
   rpgMap Map;
       //....
       //some code
       //...
   return Map;
}
// This works until i include maps.h in another .c file

当我在 eg 中包含 maps.h 时,这就是内容。 game.c 或 game.h 我有这个错误;

错误:“枚举属性”的嵌套重新定义

我不明白!

最佳答案

你需要在你的头文件中添加头保护,否则你会得到多个声明。

例如,为你的 maps.h 用这个包围它:

#ifndef MAPS_H
#define MAPS_H

...


#endif

关于c - 枚举和结构定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12886227/

相关文章:

c - 从 linux c 中的文件描述符重定向 stdin

python - 如何将程序的 STDOUT 重定向到 GDB 调试程序的 STDIN?

c++ - 在 C 源文件中包含带有命名空间的 C++ 头文件导致编译错误

java - 枚举类型的约束泛型类型以实现某些接口(interface)

swift - 使用通用关联类型扩展协议(protocol)

C - 表达式必须是可修改的左值

c - 关于宏

c++ - gcc v3.4.6 是否提供 tr1 header ?

java - 从 Spring Boot 应用程序的服务层中的请求获取 header

swift - 结果枚举被其他库覆盖