Search code examples
c++inheritanceincludevirtual-functionsundeclared-identifier

Swarmed with identifier errors


I've been programming a Monopoly game for a final project. So I thought I was on a roll, and that I had everything figured out with my psuedocode. But, it seems I forgot how to deal with includes properly, I know that is the issue since I was able to refine it to that point, but I'm not sure how to fix it.

In this super stripped down version of my code I have three .h files "Space.h" which is an abstract/virtual class which has to be inherited by a variety of different spaces that can appear on a typical Monopoly board: properties, jail, taxes, Chance, Community Chest, etc. The function that has to be inherited is run(Player&) which is what is "run" when you land on that particular space on the board, all functions that use run use a player passed by argument.

#pragma once
#include <string>
#include "Player.h"

class Space
{
public:
    virtual void run(Player&) = 0;
};

My second .h file is the "Property.h" this inherits from Space

#pragma once
#include "Space.h"

class Property : Space
{
public:
    void run(Player&) override;
    int i{ 0 };
};

Lastly I have the "Player.h" which has two variables a name and a vector of properties it owns.

#pragma once
#include <string>
#include <vector>
#include "Property.h"

class Player
{
public:
    std::string name{ "foo" };
    void addProperty(Property p);
private:
    std::vector <Property> ownedProperties;
};

Here's a very basic Property implementation

#include "Property.h"
#include <iostream>

void Property::run(Player & p)
{
    std::cout << p.name;
}

Player implementation

#include "Player.h"
#include <iostream>

void Player::addProperty(Property p)
{
    ownedProperties.push_back(p);
}

And finally main

#include "Player.h"
#include "Space.h"
#include "Property.h"

int main()
{
    Player p{};
    Property prop{};
    prop.run(p);
    system("pause");
}

Every time this is run I get a slew of errors, I'm sure it's got to do something with the circular include logic, with player including property, and property including space, which includes player. But, I don't see a workaround considering #include is needed to know how everything is defined isn't? Or are these errors referring to something else?

enter image description here


Solution

  • You have a circular include problem. Player includes Property which includes Space which includes Player again.

    You can break the circle by not including Player.h in Space.h and only forward declare the class

    #pragma once
    
    class Player;
    
    class Space
    {
    public:
        virtual void run(Player&) = 0;
    };