《敌人》没有在这个范围内声明?
好的,这就是我的错误:“Enemy”未在此范围内声明。错误位于 map.h 文件中,即使 map.h 包含敌人.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
任何人都知道发生了什么并且如何修复它?
Okay, so thats my error: 'Enemy' was not declared in this scope.The error is in the map.h file, even though map.h includes enemy.h as shown
#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
And here is enemy.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
Anyone know whats going on and how to fix it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
enemy.h
包含map.h
但是,
map.h
包含enemy.h
因此,如果包含 < code>enemy.h,处理将如下所示:
要修复此问题,请从
enemy.h
中删除#include "map.h"
,并将其替换为前向声明,class Map;
您还需要修改
void update(const Map& lv);
-- 使用 const&并在
enemy.cpp
中包含“map.h”enemy.h
includesmap.h
but,
map.h
includesenemy.h
So, if you include
enemy.h
, the processing will go something like this:to fix this, remove
#include "map.h"
fromenemy.h
, and replace it with a forward declaration,class Map;
You will also need to modify
void update(const Map& lv);
-- use a const&and include "map.h" in
enemy.cpp
您需要删除
#include
语句之一来打破循环引用。 例如,为了允许代码编译,您可以将所包含的类之一声明为Enemy.hpp 文件顶部的简单定义,然后将标头包含在 cpp 文件中。
You need to remove one of the
#include
statements to break the circular reference. To allow the code to compile you can declare one of the included classes as just a simple definitionin the top of the Enemy.hpp file, for example, and then include the header in the cpp file.
您的包含中存在循环依赖关系:
map.h
包含enemy.h
且enemy.h
包含map.h
您必须删除循环包含。
There is a circular dependency in your includes:
map.h
is includingenemy.h
andenemy.h
is includingmap.h
You must remove the circular inclusion.