枚举和结构定义

Mat*_*ath 2 c enums struct header redefinition

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

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

maps.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);
Run Code Online (Sandbox Code Playgroud)

maps.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
Run Code Online (Sandbox Code Playgroud)

所以这就是我在例如中包含maps.h时的内容。game.c或game.hi出现此错误;

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

我不明白!

oua*_*uah 5

您需要在头文件中添加头保护,否则您将获得多个声明。

例如,用以下方法将其maps.h包围起来:

#ifndef MAPS_H
#define MAPS_H

...


#endif
Run Code Online (Sandbox Code Playgroud)