2013-12-02 6 views
1

Привет, Я делаю игру для своего класса cs, и я получаю действительно странную ошибку, которую я не понимаю. Я делаю функцию, которая принимает целое число и ссылку на другой тип, который я сделал.Тип Не объявлено C++

Ошибка:

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 

Что вы думаете причины этого? Я включаю файл заголовка сетки, я думал, что это должно быть хорошо?

+1

Не помещайте 'use' директивы в файлы заголовков. – Oswald

ответ

4

У вас есть круговая зависимость между PC.h и Grid.h.

Сетка требует ПК, для которого требуется Сетка. По крайней мере, один путь, Grid или PC пока недоступен.

Использовать форвардные декларации или рефакторинг.

+0

Так что я должен сделать класс Grid; в PC.h? – user3055641

+0

@ user3055641: Вы не показали нам PC.h, но, пока существуют некоторые другие условия, да. Попробуй. –

+0

класс ПК наследуется от класса Персонаж, когда я пересылаю объявление Grid, компилятор кричит на меня, говоря В файле, прилагаемом от Grid.h: 4: 0, от testmain.cc:1: PC.h: 9: 28: error: ожидаемое имя класса перед '{' токеном – user3055641

Смежные вопросы