《敌人》没有在这个范围内声明?

发布于 2024-11-01 01:56:33 字数 1437 浏览 1 评论 0原文

好的,这就是我的错误:“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

暖心男生 2024-11-08 01:56:33

enemy.h 包含 map.h

但是, map.h 包含 enemy.h

因此,如果包含 < code>enemy.h,处理将如下所示:

  • ENEMY_H_INCLUDED 被定义
  • global.h 被包含
  • map.h 被包含
    • MAP_H_INCLUDED 已定义
    • enemy.h 被包含在内
      • ENEMY_H_INCLUDED 已定义,因此我们跳到文件末尾
    • 类 Map 被定义
      • 错误,敌人尚未定义

要修复此问题,请从 enemy.h 中删除 #include "map.h",并将其替换为前向声明, class Map;

您还需要修改 void update(const Map& lv); -- 使用 const&

并在 enemy.cpp 中包含“map.h”

enemy.h includes map.h

but, map.h includes enemy.h

So, if you include enemy.h, the processing will go something like this:

  • ENEMY_H_INCLUDED gets defined
  • global.h is included
  • map.h is included
    • MAP_H_INCLUDED gets defined
    • enemy.h gets included
      • ENEMY_H_INCLUDED is already defined, so we skip to the end of the file
    • class Map gets defined
      • error, Enemy has not been defined yet

to fix this, remove #include "map.h" from enemy.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

瞄了个咪的 2024-11-08 01:56:33

您需要删除#include 语句之一来打破循环引用。 例如,为了允许代码编译,您可以将所包含的类之一声明为

class Map;

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 definition

class Map;

in the top of the Enemy.hpp file, for example, and then include the header in the cpp file.

枯叶蝶 2024-11-08 01:56:33

您的包含中存在循环依赖关系:map.h 包含 enemy.henemy.h 包含 map.h

您必须删除循环包含。

There is a circular dependency in your includes: map.h is including enemy.h and enemy.h is including map.h

You must remove the circular inclusion.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文