类型未声明为c++



嗨,我正在为我的cs类制作一个游戏,我遇到了一个非常奇怪的错误,我不明白。我制作了一个函数,它接受一个整数和对我制作的另一个类型的引用。

错误为:

In file included from PC.h:3:0,
from Grid.h:4,
from testmain.cc:1:
Character.h:23:20: error: ‘Grid’ has not been declared

字符.h

#ifndef __CHARACTER_H__
#define __CHARACTER_H__
#include "Entity.h"
#include "Grid.h"
using namespace std;
class Character : public Entity{
int hp;
int atk;
int def;
char prev;
public:
Character(int x, int y, char s, int hp, int atk, int def);
int getHp();
int getAtk();
int getDef();
void setHp(int i);
void setAtk(int i);
void setDef(int i);
void attack(Character &c);
bool move(int dir, Grid &g);
};
#endif

网格.h

#ifndef __GRID_H__
#define __GRID_H__
#include "Entity.h"
#include "PC.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
using namespace std;
/*
Class Grid is only created once per game, it holds information about the level layout
and the population of the grid (ie.Entities).
*/
class Grid{

int sLoc[25][79];               //holds potential spawn locations
int rNum[5];                    //holds the number of spawnable    locations in each room
int level;                      //the current level, used for     reading in the maps/current level
int playerRoom;             //holds the room that the player will spawn in, necessary for stair-spawn restrictions
int dragons;                    //hold number of spawned dragons
void readLevel();               //reads in the current level from a text file in the same dir. with the correct name ("1.txt", "2.txt"...)  
void addHero(PC &hero);     //takes in an hero and adds it to the array of Entities
void addStairs();               //adds the stairs in a different room than the player
void addGold();             //distributes gold across the board
void addPots();             //distributes potions across the board
void addtheD(int x, int y);//adds dragons
void addMonsters();         //adds monsters
void checkRNum();               //fills in rNum with the corresponding     amount of tiles per room
void readLoc();             //identifies where all rooms begin and end
void printLoc();                //prints the sloc arrays, for testing p   urposes
void clear();                   //cleares all entities left on the board and takes care of memory deletion
void populate(PC &hero);    //populates the board with rng monsters
public:
Grid();                         //default constructor, no parameters necessary as every game starts at level one with no population or map
void gg(PC &hero);          //increases level by 1 (goes to next level)
void printBoard();          //prints out the map layout and the symbols that indicate which Entites are present
char boardAt(int x, int y);
char Board[25][79];         //the maps layout as well as information about which     Entity occupies a given position (if any)
Entity* population[25][79];//holds pointers to the Entities that populate the board
};
#endif

你认为是什么原因造成的?我包括了网格头文件,我想这应该可以吗?

PC.hGrid.h之间存在循环依赖关系。

网格需要PC,而PC需要网格。至少在某种程度上,GridPC还不可用。

使用正向声明或重构。

最新更新