"Enemy"是否未在此范围内宣布?

Cya*_*ime 2 c++ circular-dependency include

好的,这就是我的错误:'Enemy'未在此范围内声明.错误在map.h文件中,即使map.h包含enemy.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
Run Code Online (Sandbox Code Playgroud)

这是敌人

#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
Run Code Online (Sandbox Code Playgroud)

有谁知道发生了什么以及如何解决它?

Tim*_*Tim 6

enemy.h 包括 map.h

但是,map.h包括enemy.h

所以,如果你包括enemy.h,处理将是这样的:

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

修复此问题,#include "map.h"enemy.h远程声明中删除并替换它,class Map;

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

并包括"map.h" enemy.cpp