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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mrjohn
    New Member
    • May 2009
    • 31

    #1

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

    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.

    [code="C++"]//---------------------------------------------------------------------------
    // A map tile
    //---------------------------------------------------------------------------
    #ifndef TILE_H
    #define TILE_H

    #include <string>
    #include "Actor.h"
    using namespace std;

    class Tile
    {
    public:
    //-----------------------------------------------------------------------
    // Default Constructor
    Tile();

    //-----------------------------------------------------------------------
    // Copy Constructor
    Tile(const Tile&);

    //-----------------------------------------------------------------------
    // Destructor
    ~Tile();

    //-----------------------------------------------------------------------
    // Assignment Operator
    const Tile& operator=(const Tile&);

    //-----------------------------------------------------------------------
    // Getters/Setters
    string getName() const;
    void setName(const string);

    int getX() const;
    void setX(const int);
    int getY() const;
    void setY(const int);

    int getImgX() const;
    void setImgX(const int);
    int getImgY() const;
    void setImgY(const int);

    Actor* getOccupant() const;
    void setOccupant(Act or*);

    private:
    //The name, or index of the tile
    string name;

    //The coordinates of the tile on the map
    int x;
    int y;

    //The coordinates of the tile image within the graphics file
    int imgX;
    int imgY;

    Actor* occupant;
    //ArrayList Items
    };

    #endif[/code]

    [code="c++"]//---------------------------------------------------------------------------
    // The basic building block for players and monsters
    //---------------------------------------------------------------------------
    #ifndef ACTOR_H
    #define ACTOR_H

    #include <string>
    #include "Tile.h"
    using namespace std;

    class Actor
    {
    public:
    Actor();
    Actor(Actor&);
    ~Actor();

    protected:
    Tile* position;
    int health;
    int maxHealth;
    int strength;
    int dexterity;
    string name;
    int imgX;
    int imgY;
    string imageName;
    };

    #endif[/code]

    For some reason, I get the error:

    1>c:\c++\tilequ est\tilequest\a ctor.h(19) : error C2143: syntax error : missing ';' before '*'
    1>c:\c++\tilequ est\tilequest\a ctor.h(19) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    1>c:\c++\tilequ est\tilequest\a ctor.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?
  • Bala kumar
    New Member
    • Oct 2010
    • 2

    #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.

    Comment

    • mrjohn
      New Member
      • May 2009
      • 31

      #3
      That worked like a charm. Thanks.

      Comment

      • Scott D Bowen

        #4
        You could, and probably should, consider:

        #ifndef
        #define <something>

        ...

        #endif

        Comment

        Working...