Thread: Having big problems with classes

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    42

    Having big problems with classes

    I'm making a heavily object oriented text based RPG as a refinement of my skills (I've posted a lot on the game and C++ boards because of problems I've had).

    I've been neatening up my code and making it easy to manipulate before I put in more code. However, it seems I've gotten into a problem with including headers again.

    Here's the basic structure of my code and what references what.

    Character (class)
    - Has a location (a Room pointer)
    - Has an inventory (an Inventory pointer)

    Item (class)
    - Has a destination (a Destination pointer)
    - The destination is used if the item can be the target of a "go" command
    - Has a "next" item when in an inventory (an Item pointer)

    Room (class)
    - Has an array of exits (Room pointers)
    - Has items on the ground (Item pointers)

    Inventory (class)
    - Has a head and tail (pointers to Items)

    I'm trying to use the Destination class because I don't want items to have pointers to rooms.

    Destination (class)
    - Has "exits" that are accessed with the "go" command (pointers to Rooms)


    Here's my problem. When I try to #include the header for Destination in the Item class, I get an error in the Room class that it doesn't understand what an Inventory is. Not only that, it even doesn't know what an Item is.

    The error:

    In file included from Destination.h
    from Item.h
    from Inventory.h
    from Character.h
    from Character.cpp
    -here it gives a syntax error on the line where I declare the Inventory for the Room class

    I'm starting to really hate #include. Isn't there some way to tell my code "Shut up. These classes exist, you idiot. Look first before you tell me they don't."? It seems that while following all of my #includes, the compiler gets confused.

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    42
    I forgot to post the source. There's a bit of mess in the actual code (I haven't finished neatening it up), but I'm pretty sure the problem is in my implimentation of #include anyway.

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    1,640
    You're including a .CPP file,wrong.
    You should include .H files with the declarations of the CPP file.

  4. #4
    Registered User
    Join Date
    Jan 2003
    Posts
    42
    Do you mean that when I include the header from another class, I should put the #include in the .cpp file, not the .h file?

  5. #5
    Registered User
    Join Date
    Sep 2002
    Posts
    1,640
    You should put the decleration of a class in a header file and
    include that header file in the cpp file where theyre defined and
    include it where theyre used

  6. #6
    Registered User
    Join Date
    Jan 2003
    Posts
    42
    That's exactly what I do, though. I have the class and its prototypes in a .h file, and then the functions themselves in a .cpp, with an #include of the header at the top.

    My current working theory is that I'm confusing the compiler.

    Inventory includes Item, Item includes Destination, Destination includes Room, so could it possibly be that the compiler reads the prototypes for Inventory before it actually reads in that Inventory is a class, thus confusing it when it sees a private Inventory variable in Room?

  7. #7
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Code:
    myclass.hpp
    #include <otherdependancies>
    
    #ifndef MYCLASS_H_
    #define MYCLASS_H_
    
    class myclass
    {
    public:
       myclass();
       ~myclass();
    private:
       int m_int;
    };
    
    #endif

    Code:
    myclass.cpp
    #include "myclass.hpp"
    
    myclass::myclass()
    {
       m_int = 0;
    }
    
    myclass::~myclass()
    {
       // Stuff
    }
    Code:
    main.cpp
    #include <otherdependancies>
    #include "myclass.hpp"
    
    int main(int argc, char **argv[])
    {
    
    }

  8. #8
    Registered User
    Join Date
    Jan 2003
    Posts
    42
    I tried your suggestion, Eibro, but it seems to have sent my compiler spiriling into an infinite loop.

    The #include's in Item, Room, and Destination all seem to lead the compiler in a big circle.

    In file included in Destination.h
    from Item.h
    from Room.h
    from Destination.h
    from Item.h
    from Room.h
    from Destination.h
    from ...

  9. #9
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753
    You're right. Its a big loop. EX: Item.h includes room.h, room.h includes inventory.h, and inventory.h includes item.h. something like that.

    Perhaps you should compile your code more often. Then you wouldn't have ****319**** freaking errors to debug!!!

  10. #10
    Registered User
    Join Date
    Jan 2003
    Posts
    42
    Where'd you get 319 from?

    I can't figure out how to break out of that messed up loop. Room HAS to print out the items in the room, and in order for doors and other "goables" to work, they HAVE to in some way know what room to go to. Right now I'm sketching out my class arrangement on paper, because that's a nice way to make sure everything is on a one way street...

  11. #11
    someguy
    Guest
    are you using:

    #ifndef MYCLASS_H_
    #define MYCLASS_H_

    this will prevent classes from being included more than once.

  12. #12
    Registered User
    Join Date
    Jan 2003
    Posts
    42
    Yup. For every single class I use that.

    -edit-
    I think I may solve this problem by using integer values to designate the ID of a room, item, or character, and then having a static vector in each class that can be used to keep track of my objects. That way, rooms won't store items. They'll store the IDs of the items they have. Items won't have rooms as their go destinations. They'll have the ID of the rooms. Etc.
    Last edited by Vorok; 01-09-2003 at 10:30 PM.

  13. #13
    319 is the amnt of errors we get when compiling your code on MSVC++ 6

    I cut down on these errors a bunch by including some STD headers you omitted and some using statements.

    Whenever you use COUT make sure to include <iostream> in the header of the class. Also, put the statement "using namespace std;" underneath that header.

    For <string> put the "using std::string;" statement under header.

    I'm still working on it, I'll see why those custom classes are not being recognized, but I've cut down the errors to 67 now.
    My Avatar says: "Stay in School"

    Rocco is the Boy!
    "SHUT YOUR LIPS..."

  14. #14
    Registered User
    Join Date
    Jan 2003
    Posts
    42
    I'll do that for now on. Sorry for my lack of compliance with those standards. I'm still trying to get all of those into my head.

    -edit-
    I've finally got this working. What I did was use a static vector for the three classes that keeps track of every new instance of it. That way I can access the rooms and items and characters by their ID numbers instead of by actual pointers.
    Last edited by Vorok; 01-10-2003 at 12:55 AM.

  15. #15
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    One thing I notice is that take for example this
    file
    Code:
    /*********************
    * CHARACTER PROTOTYPES
    **********************/
    #ifndef CHARACTER_CLASS
    #define CHARACTER_CLASS
    #include <string>
    #include "Inventory.h"
    #include "Destination.h"
    #include "Item.h"
    #include "Room.h"
    
    class Character{
      private:
        string name;
        string desc;
        bool isPlayer;
        string charDesignation;
        int maxHP, HP;  //hit points
        int maxMP, MP;  //mana power
        int maxST, ST;  //stamina
        Room* location; //location of character
        int stance;     //0 = standing | 1 = sitting | 2 = laying down
        int hand[2];    //[0]=left | [1]=right, -1 = empty hand
        Inventory inventory;
    
      public:
        Character();
        ~Character();
    
        void setName(const string &newName);
        void setDesc(const string &newDesc);
        void setLocation(Room *newLocation);
        void addItem(Item *newItem);
        void removeItem(Item *target);
        void setHand(int i, int id);
        void setIsPlayer(bool status);
    
        string getName();
        string getDesc();
        Room* getLocation();
        int getHand(int i);
    
        void printInventory();
        void move(int dir);
        void go(string itemName);
        void get(string itemName);
        void drop(string itemName);
        void open(string itemName);
        void close(string itemName);
    };
    #endif
    You don't need #include "item.h" or any of the classes
    that you only use as a referance. Only then in your .cpp file
    that calls functions from item.h and
    room.h you include those files.


    Code:
    #ifndef CHARACTER_CLASS
    #define CHARACTER_CLASS
    #include <string>
    #include "Inventory.h"
    
    class Room;
    class Item;
    
    class Character{
      private:
        string name;
        string desc;
        bool isPlayer;
        string charDesignation;
        int maxHP, HP;  //hit points
        int maxMP, MP;  //mana power
        int maxST, ST;  //stamina
        Room* location; //location of character
        int stance;     //0 = standing | 1 = sitting | 2 = laying down
        int hand[2];    //[0]=left | [1]=right, -1 = empty hand
        Inventory inventory;
    
      public:
        Character();
        ~Character();
    
        void setName(const string &newName);
        void setDesc(const string &newDesc);
        void setLocation(Room *newLocation);
        void addItem(Item *newItem);
        void removeItem(Item *target);
        void setHand(int i, int id);
        void setIsPlayer(bool status);
    
        string getName();
        string getDesc();
        Room* getLocation();
        int getHand(int i);
    
        void printInventory();
        void move(int dir);
        void go(string itemName);
        void get(string itemName);
        void drop(string itemName);
        void open(string itemName);
        void close(string itemName);
    };
    #endif

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. College Classes
    By nubby in forum C Programming
    Replies: 2
    Last Post: 10-07-2007, 12:32 AM
  2. Inheritance, overriding, problems
    By CodeMonkey in forum C++ Programming
    Replies: 8
    Last Post: 01-04-2007, 01:26 AM
  3. classes, inheritance and include problems
    By baniakjr in forum C++ Programming
    Replies: 6
    Last Post: 12-12-2006, 01:45 PM
  4. C++ Classes: Use, Misuse...Confusion.
    By Snorpy_Py in forum C++ Programming
    Replies: 4
    Last Post: 10-23-2006, 01:46 AM
  5. Big and little endian
    By Cactus_Hugger in forum C Programming
    Replies: 4
    Last Post: 10-12-2005, 07:07 PM