-
Headers and Classes
Okay, I'm making an RPG (I know, who isn't?)
However, I hadn't run into any problems I couldn't fix until now.
Here is the code that most likely has the bug in it.
Code:
//item.h"
#include "player.h"
#ifndef H_ITEM
#define H_ITEM
class Item
{
public:
int cost;
protected:
Player *owner;
};
class equipment : public Item
{
bool equipped;
virtual void equip(void);
};
class weapon : public equipment
{
int strbonus;
};
class armor : public equipment
{
int defbonus;
};
#endif
//player.h//
#include "item.h"
#ifndef H_PLAYER
#define H_PLAYER
class creature
{
public:
char name[15];
char desc[50];
int hp;
int mf;
Item *inv[20];
creature(char n[15],int s,int a,int v, int e);
~creature();
protected:
int mmf;
int mhp;
int str;
int agi;
int vit;
int enr;
};
class Monster : public creature
{
int soul_type;
};
class Player : public creature
{
public:
Player(char name[15],int s,int a,int v,int e);
~Player();
int gold;
int souls;
bool SaveChara(char *filename);
bool LoadChara(char *filename);
};
#endif
Sorry about the bad identation, and the lack of knowledge on my part...
EDIT:
Forgot to post the compiling errors.
! player.h(13,13): Type name expected
! player.h(13,13): Declaration missing ;
-
I'm amazed you even get that error. You include player.h, that includes item.h, that includes player.h, that includes item.h, that includes player.h, that includes...
You should put the includes inside the inclusion guards (#ifndef/#define).
Second, you still have double inclusion meaning both includes each others. Not impossible, but can make it a bit trickier. In your case the Item doesn't depend on the player, you could remove "include "player.h" from item.h.
If you still need it, this is a way to handle it:
Item.h:
Code:
#ifndef/#define yada...
class Player; //Tell there is a player class, don't define it yet
class Item
{
...
};
//HERE you include the player class definition
#include "Player.h"
Player.h:
Code:
#ifndef/#define yada...
#include "Item.h"
class Player
{
...
};
-
thnx
so you're saying that i need to include the "item.h"/"player.h" inside the #ifndef? And I need to put class Player/Item inside too?
thnx, again