0

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

Vishal Vasani
  • 647
  • 8
  • 16
PaddleStroke
  • 63
  • 1
  • 6
  • 1
    `world.h` includes `object.h` which includes `world.h`... This is a circular dependency, and the only way to break it is to *stop* the inclusion and to use *forward declarations* instead. For example `world.h` doesn't need the whole `object.h` header file, only the declaration of the `Object` class. So change `#include "object.h"` to `class Object;`. You can do a similar thing with `object.h` and the `World` class. – Some programmer dude Jul 27 '22 at 12:07
  • Thanks ! Just changing #include "object.h" to class Object; solved it. Though I cannot replace #include "world.h" by class World; because in Agent some functions use vars from world (ie world->sizeX). – PaddleStroke Jul 27 '22 at 12:12
  • ***I cannot replace #include "world.h" by class World; because in Agent some functions use vars from world (ie world->sizeX).*** If the functions are not defined in `Agent.h` forward declare World and put the `#include "world.h"` in `Agent.cpp` – drescherjm Jul 27 '22 at 13:16
  • by the way this has nothing to do with classes per se, and everything to do with header files. No rule says you have to have one header file per class. – user253751 Jul 27 '22 at 13:36

0 Answers0