没有在此范围内声明“敌人”吗?

问题描述

| 好的,这就是我的错误:在此范围内未声明\'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
您必须删除圆形包含物。