嗨,我正在为我的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
Character.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
Grid.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
您认为这会导致什么?我包括网格头文件,我认为这应该没问题?
答案 0 :(得分:4)
PC.h
和Grid.h
之间存在循环依赖关系。
网格需要PC,这需要网格。至少有一种方法,Grid
或PC
尚不可用。
使用前向声明或重构。