I have 5 classes and have trouble including them correctly. I have build errors.
myFrame : manage the UI and display the 'World' which is composed of 'Objects'.
myFrame.cpp
#include "cMain.h"
MyFrame::MyFrame() : wxFrame(...)
{
...
world = new World();
for (int i = 0; i < 20; i++) {
Agent* agent = new Agent(world, ID_Agent, rand(), rand(), newId());
world->objects.push_back(agent);
}
for (int i = 0; i < 10; i++) {
Object* obj = new Object(world, ID_Object, rand(), rand(), newId());
world->objects.push_back(obj);
}
}
myFrame.h
#pragma once
#include "wx/wx.h"
#include "World.h"
#include "Object.h"
#include "Agent.h"
class MyFrame : public wxFrame
{
public:
MyFrame(...);
...
World* world;
};
This world object contains a vector of objects.
World.cpp
#include "World.h"
World::World() {}
World.h
#pragma once
#include <vector>
#include "Object.h"
class World
{
public:
World();
std::vector<Object*> objects;
};
Objects.cpp
#include "Object.h"
Object::Object(World* w, int t, double pX, double pY)
{
world = w;
type = t; posX = pX; posY = pY;
}
object.h
#pragma once
#include "World.h"
class Object
{
public:
Object(World* w, int t, double pX, double pY);
double posX, posY; int type, id;
World* world;
};
And Agent, which derives from Object : Agent.cpp
#include "Agent.h"
Agent::Agent(World* w, int t, double pX, double pY) : Object(w, t, pX, pY) {}
Agent.h
#pragma once
#include "Object.h"
class Agent : public Object
{
public:
Agent(World* world, int t, double pX, double pY);
};
It was working until I had to pass a 'world' pointer to the 'Agent' object. Such that each agent could access data from other agents/the world. So I added '#include "World.h"' in object.h and added World* w to agent's ctor. Since it cannot build. I have errors like - 'Object' undeclared identifier - in world.h, among others. I guess that world and object including each other is the problem but I can't find how to fix it. Any hint would be awesome! Thanksww