使用代码块编译我的项目时出现错误。 我的问题来自枚举定义和结构定义。
它们是在头文件中定义的机器人,因为我只在 .c 关联文件中使用那些枚举和结构,所以它起作用了。但是当我将 .h 文件包含在另一个 .c 文件中时,出现错误,这是一些代码;
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);
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
当我在 eg 中包含 maps.h 时,这就是内容。 game.c 或 game.h 我有这个错误;
错误:“枚举属性”的嵌套重新定义
我不明白!
最佳答案
你需要在你的头文件中添加头保护,否则你会得到多个声明。
例如,为你的 maps.h
用这个包围它:
#ifndef MAPS_H
#define MAPS_H
...
#endif
关于c - 枚举和结构定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12886227/