简体   繁体   中英

C++: Including header files in each other

So I have seen this question asked, but the examples people provided were extremely simple (their classes had no constructors or methods) and I don't know how to extend the solutions to a more complex case.

I have tried using forward declarations and pointers, just forward declarations, just pointers, and even forward declarations and typename definitions, all of which were suggested solutions from the other simpler posts and none of which have worked (unknown identifier or incomplete type errors). So how would I get the two files below to compile correctly and be used as I intended?

Unit.hpp:

#ifndef PROJECT_UNIT_HPP
#define PROJECT_UNIT_HPP

#include "GridNode.hpp"

class Unit
{
private:
    /* Fields */
    int xPos, yPos, ownerID;
    std::vector<GridNode> influenceMap;

public:
    /* Constructors */
    Unit(int x, int y, int id) : xPos(x), yPos(y), ownerID(id)
    {
        influenceMap.push_back( GridNode() );
    }

    /* Methods */
    std::vector<GridNode> getMap() {return influenceMap;}
};

#endif

GridNode.hpp:

#ifndef PROJECT_GRIDNODE_HPP
#define PROJECT_GRIDNODE_HPP

#include "Unit.hpp"

class GridNode
{
private:
    /* Members */
    int id;
    std::vector<Unit> nodeUnits;

public:
    /* Static vars */
    static int nodeLength;

    /* Constructors */
    GridNode()
    {
        std::cout << "Reached here!\n";
    }
};

#endif

All you need to do is #include <vector> in both and forward declare class Unit; in GridNode.hpp :

#ifndef PROJECT_GRIDNODE_HPP
#define PROJECT_GRIDNODE_HPP

// using std::vector
#include <vector>

// Forward declare
class Unit;

class GridNode
{
private:
    /* Members */
    int id;
    std::vector<Unit> nodeUnits;

public:
    /* Static vars */
    static int nodeLength;

    /* Constructors */
    GridNode()
    {
        std::cout << "Reached here!\n";
    }
};

#endif

You need forward declarations AND to move member function bodies (including constructors and destructors) out of the class body, and after inclusion of the other class definition.

Even implicit constructors and destructors will break things, you'll need explicit user-provided declarations (although you can use the compiler-provided definitions via = default )

class GridNode;
class Unit
{
private:
    /* Fields */
    int xPos, yPos, ownerID;
    std::vector<GridNode> influenceMap;

public:
    /* Constructors */
    Unit(int x, int y, int id);
    Unit(const Unit&);
   ~Unit();

    /* Methods */
    std::vector<GridNode> getMap();
};

#include "GridNode.hpp"

inline Unit::Unit(int x, int y, int id) : xPos(x), yPos(y), ownerID(id)
{
    influenceMap.push_back( GridNode() );
}

inline Unit::Unit(const Unit&) = default;

inline Unit::~Unit() = default;

inline std::vector<GridNode> Unit::getMap() {return influenceMap;}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM