我正在用SDL 2.0
C开发游戏,但遇到以下问题:每个文件包含所有需要的.h文件后,编译器在上显示错误(unknown type name 'Enemy'
)shoots.h
,其中我有一个函数类型声明为Enemy敌人
我认为我遇到错误的头文件是bullet.h,敌人.h,mobs.h和shoots.h。(还有更多类似sdl.h
)
项目符号
#ifndef BULLET_H_INCLUDED
#define BULLET_H_INCLUDED
#include "sdl.h"
typedef struct Bullet *Bullet;
#endif // BULLET_H_INCLUDED
敌人
#ifndef ENEMY_H_INCLUDED
#define ENEMY_H_INCLUDED
#include "sdl.h"
#include "shoots.h"
typedef struct Enemy *Enemy;
#endif // ENEMY_H_INCLUDED
暴徒
#ifndef MOBS_H_INCLUDED
#define MOBS_H_INCLUDED
#include "enemy.h"
typedef struct EnemyList *EnemyList;
typedef struct EnemyPosition *EnemyPosition;
#endif // MOBS_H_INCLUDED
芽.h
#ifndef SHOOTS_H_INCLUDED
#define SHOOTS_H_INCLUDED
#include "sdl.h"
#include "player.h"
#include "bullet.h"
#include "enemy.h"
typedef struct BulletList *BulletList;
typedef struct BulletPosition *BulletPosition;
void initEnemyShoots(BulletList l, BulletPosition p, Player pl, Enemy e);
#endif // SHOOTS_H_INCLUDED
在敌人上构造敌人声明
struct Enemy {
SDL_Surface *sprite; //Enemy sprite
int x, y; //Enemy position
int w, h; //Enemy hitbox
BulletList ammo; //Enemy bullets
};
这些标头还声明了在每个.c文件上实现的功能。所有结构都在他自己的.c中定义
那是我遇到问题的地方initEnemyShoots,因为其中一个参数是Enemy类型。我该如何解决?
该问题是由于敌方.h中包含#shoots.h而引起的
在#include "shoots.h"
后面#define ENEMY_H_INCLUDED
,但是它在实际声明Enemy
为指向的指针之前struct Enemy
。因此,shoots.h内部Enemy
尚未定义,但是ENEMY_H_INCLUDED
,即使shoots.h #includes敌人.h,include防护也阻止对其进行评估,并且Enemy
永远都不会被定义。
解决方案是,如果不需要的话,不要在敌人.h内部包含“ shoots.h”。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句