By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,334 Members | 2,746 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,334 IT Pros & Developers. It's quick & easy.

Can I have two .h files that #include each other?

mrjohn
P: 32
I've got two classes, and each one has a pointer to another. However, I'm having trouble with one of them. Actor.h is acting like the other class doesn't exist.

Expand|Select|Wrap|Line Numbers
  1. //---------------------------------------------------------------------------
  2. // A map tile
  3. //---------------------------------------------------------------------------
  4. #ifndef TILE_H
  5. #define TILE_H
  6.  
  7. #include <string>
  8. #include "Actor.h"
  9. using namespace std;
  10.  
  11. class Tile
  12. {
  13. public:
  14.     //-----------------------------------------------------------------------
  15.     // Default Constructor
  16.     Tile();
  17.  
  18.     //-----------------------------------------------------------------------
  19.     // Copy Constructor
  20.     Tile(const Tile&);
  21.  
  22.     //-----------------------------------------------------------------------
  23.     // Destructor
  24.     ~Tile();
  25.  
  26.     //-----------------------------------------------------------------------
  27.     // Assignment Operator
  28.     const Tile& operator=(const Tile&);
  29.  
  30.     //-----------------------------------------------------------------------
  31.     // Getters/Setters
  32.     string getName() const;
  33.     void setName(const string);
  34.  
  35.     int getX() const;
  36.     void setX(const int);
  37.     int getY() const;
  38.     void setY(const int);
  39.  
  40.     int getImgX() const;
  41.     void setImgX(const int);
  42.     int getImgY() const;
  43.     void setImgY(const int);
  44.  
  45.     Actor* getOccupant() const;
  46.     void setOccupant(Actor*);
  47.  
  48. private:
  49.     //The name, or index of the tile
  50.     string name;
  51.  
  52.     //The coordinates of the tile on the map
  53.     int x;
  54.     int y;
  55.  
  56.     //The coordinates of the tile image within the graphics file
  57.     int imgX;
  58.     int imgY;
  59.  
  60.     Actor* occupant;
  61.     //ArrayList Items
  62. };
  63.  
  64. #endif
Expand|Select|Wrap|Line Numbers
  1. //---------------------------------------------------------------------------
  2. // The basic building block for players and monsters
  3. //---------------------------------------------------------------------------
  4. #ifndef ACTOR_H
  5. #define ACTOR_H
  6.  
  7. #include <string>
  8. #include "Tile.h"
  9. using namespace std;
  10.  
  11. class Actor
  12. {
  13. public:
  14.     Actor();
  15.     Actor(Actor&);
  16.     ~Actor();
  17.  
  18. protected:
  19.     Tile* position;
  20.     int health;
  21.     int maxHealth;
  22.     int strength;
  23.     int dexterity;
  24.     string name;
  25.     int imgX;
  26.     int imgY;
  27.     string imageName;
  28. };
  29.  
  30. #endif
For some reason, I get the error:

1>c:\c++\tilequest\tilequest\actor.h(19) : error C2143: syntax error : missing ';' before '*'
1>c:\c++\tilequest\tilequest\actor.h(19) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\c++\tilequest\tilequest\actor.h(19) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

However, there is no error if I change "Tile* position" to "int* position". It almost seems as if it doesn't recognize Tile as a class. Could this be because they're both #including each other? What am I doing wrong?
Oct 11 '10 #1
Share this Question
Share on Google+
3 Replies


P: 2
Give a forward declaration of one of the class in other file and include the header in *.cpp file.

This should solve ur issue.
Oct 11 '10 #2

mrjohn
P: 32
That worked like a charm. Thanks.
Oct 14 '10 #3

P: n/a
You could, and probably should, consider:

#ifndef
#define <something>

...

#endif
Oct 21 '10 #4

Post your reply

Sign in to post your reply or Sign up for a free account.