如何解决没有在此范围内声明“敌人”吗?
| 好的,这就是我的错误:在此范围内未声明\'Enemy \'。错误在map.h文件中,即使map.h包含了敌人。#ifndef MAP_H_INCLUDED
#define MAP_H_INCLUDED
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include \"enemy.h\"
#define MAX_TILE_TYPES 20
using namespace std;
class Map{
public:
Map();
void loadFile(string filename);
int** tile;
int** ftile;
bool solid[MAX_TILE_TYPES];
int width;
int height;
int tileSize;
vector<Enemy> enemies;
};
#endif // MAP_H_INCLUDED
这是敌人。h
#ifndef ENEMY_H_INCLUDED
#define ENEMY_H_INCLUDED
#include \"global.h\"
#include \"map.h\"
class Enemy{
public:
Enemy();
Enemy(float nx,float ny,float nstate);
void update(Map lv);
bool rectangleIntersects(float rect1x,float rect1y,float rect1w,float rect1h,float rect2x,float rect2y,float rect2w,float rect2h);
void update();
float x;
float y;
Vector2f velo;
float speed;
float maxFallSpeed;
int state;
int frame;
int width;
int height;
int maxStates;
int *maxFrames;
int frameDelay;
bool facingLeft;
bool onGround;
bool dead;
int drawType;
};
#endif // ENEMY_H_INCLUDED
任何人都知道发生了什么事以及如何解决它?
解决方法
您需要删除“ 2”语句之一才能破坏循环引用。为了允许代码编译,您可以将包含的类之一声明为简单定义
class Map;
例如,在Enemy.hpp文件的顶部,然后在cpp文件中包含标头。
,enemy.h
包括map.h
但是,map.h
包括enemy.h
因此,如果包含enemy.h
,处理将如下所示:
ENEMY_H_INCLUDED被定义
包含global.h
包含map.h
定义了MAP_H_INCLUDED
敌人.h被包括在内
ENEMY_H_INCLUDED已经定义,因此我们跳到文件末尾
类Map被定义
错误,尚未定义敌人
要解决此问题,请从enemy.h
中删除#include \"map.h\"
,并将其替换为前向声明class Map;
您还需要修改ѭ12-使用const&
并在enemy.cpp
中包含\“ map.h \”
,您的include中有一个循环依赖项:map.h
包括enemy.h
,enemy.h
包括map.h
您必须删除圆形包含物。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。