Thread: Headers and Classes

  1. #1
    Teenage Mutant Ninja Nerd MMD_Lynx's Avatar
    Join Date
    Aug 2004
    Posts
    65

    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 ;
    Last edited by MMD_Lynx; 11-30-2004 at 05:34 PM.
    Stupid people are useful. You can make them do all the mindless tasks you are too lazy to do yourself.

    Sphynx cats are just bald and wrinkly, like old people, and we don't reject them.

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    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
    {
       ...
    };
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Teenage Mutant Ninja Nerd MMD_Lynx's Avatar
    Join Date
    Aug 2004
    Posts
    65
    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
    Stupid people are useful. You can make them do all the mindless tasks you are too lazy to do yourself.

    Sphynx cats are just bald and wrinkly, like old people, and we don't reject them.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Headers, Classes, and Functions question.
    By Zeusbwr in forum C++ Programming
    Replies: 1
    Last Post: 10-21-2004, 03:18 AM
  2. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  3. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  4. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM
  5. Redefinition of classes (#include headers)
    By cjschw in forum C++ Programming
    Replies: 3
    Last Post: 09-04-2003, 09:24 PM