I'm rather new to C++ projects. I'm trying to build a project where a lot of the classes contain fields that are references to objects of other classes. "This company owns that location, that location is occupied by these agents, these agents work for that company."
As you can imagine, it's difficult to get the compiler to work through these class definitions without hitting a loop. I always used include guards in my files, and I've tried using forward declarations in the classes that kick up errors, but I'm still dealing with issues when I try and initialize some of these objects.
Here's the class giving me issues right now:
agent.h
#ifndef AGENT_H
#define AGENT_H
#include "gameObject.h"
#include "location.h"
class Agent : public GameObject {
public:
Location* currentLocation;
GameObject* currentTarget;
};
#endif
I'm calling the constructor for Agent in my main.cpp file, and I get an error "expected class name before '{' token" for Agent : GameObject. But GameObject is included before Agent's inheritance of the class is declared.
gameObject.h
#ifndef GAMEOBJ_H
#define GAMEOBJ_H
#include <string>
#include "company.h"
class Company;
class GameObject {
public:
Company* owner;
}
#endif
GameObject only relies on a definition of the Company class to compile, so I go look at my Company class...
company.h
#ifndef COMPANY_H
#define COMPANY_H
#include "agent.h"
#include "location.h"
#include "network.h"
#include "project.h"
class Company {
public:
std::vector<Agent*> currentAgents;
std::vector<Network*> currentNetworks;
std::vector<Location*> currentLocations;
std::vector<Project*> currentProjects;
}
#endif
Company references all the child classes of GameObject, so I thought adding forward declarations of those classes here would keep the loop from happening and allow Company to fully compile, but the same list of errors come up regardless of whether or not Company.h has...
class Agent;
class Network;
class Location;
class Project;
...or not.
Any glaring issues here?