0

I have two classes : World, and Entity.

Inside of World I have two Entity pointers which I made like so:

Entity* ent1;
Entity* ent2;

I wanted to allow Entity objects to call World's public member functions. I thought was that I could simply pass in a reference or a pointer of World to the Entity.

But when I include World.h from Entity.h, I start getting errors. It does seem a bit wrong as they include each other but I have no other idea of how to achieve this functionality.

In other programming languages I've seen the parent keyword, is there anything like that in C++?

James Oravec
  • 19,579
  • 27
  • 94
  • 160
Radicate
  • 2,934
  • 6
  • 26
  • 35
  • 1
    possible duplicate of [Circular C++ Header Includes](http://stackoverflow.com/questions/1281641/circular-c-header-includes) – Billy ONeal Apr 22 '13 at 22:45
  • Probably you will need to do a forwad declaration: http://stackoverflow.com/questions/553682/when-to-use-forward-declaration – Ian Medeiros Apr 22 '13 at 22:46
  • Since you are using pointers, you don't need to include the `World` class header in `entity.h`, you only need to do a forward declaration: `class World;`. – didierc Apr 22 '13 at 22:47
  • what errors are you seeing? – TomD Apr 22 '13 at 23:33

3 Answers3

1

Forward-declare class Entity in World.h.

World.h:

class Entity; // #include "Entity.h" is not needed, because
              // only a pointer to Entity is used at the moment.

class World {
  public:
    void foo() {}

    void letEntityDoFooToMe(); // Implementation must be defined later, because it
                               // will use the whole Entity class, not just a
                               // pointer to it.
  private:
    Entity* e;
};

Entity.h:

#include "World.h" // Needed because Entity::doFooToWorld calls a method of World.

class Entity {
  public:
    Entity(World& world) : w(world) {}

    void doFooToWorld() {
      w.foo();
    }

  private:
    World& w;  
};

World.cpp:

#include "World.h"  // Needed because we define a method of World.
#include "Entity.h" // Needed because the method calls a method of Entity.

void World::letEntityDoFooToMe() {
  e->doFooToWorld();
}
Oswald
  • 31,254
  • 3
  • 43
  • 68
  • I see, but this case limits World right? As in, World won't be able to call Entity's public methods.. is there a way to change that? – Radicate Apr 22 '13 at 23:13
  • @Don I added method `World::letEntityDoFooToMe()` to show you how methods of `World` can call methods of `Entity`. – Oswald Apr 22 '13 at 23:36
0

What you could do is make the method in the parent class virtual and override it in the entity class.

class World
{
public:
    virtual void Func();
}


class Entity: public World
{
public:
    void Func();
}
masotann
  • 901
  • 10
  • 29
0

From your description my guess is that you are having some cyclic dependency problem. Have you tried to use #pragma once. Here is a reference link. If you dont like that you could also try adding some ifndef in the headers for each.

// World.h
#ifndef WORLD_H
#define WORLD_H

// ... World declaration code goes here.

#endif

// Entity.h
#ifndef ENTITY_H
#define ENTITY_H

// ... Entity declaration code goes here.

#endif
Community
  • 1
  • 1
Kunashu
  • 278
  • 1
  • 4
  • 15
  • I tried that right when I thought it might be a problem that the classes include each other, I still have the pragma once but neither of those fix the problem – Radicate Apr 22 '13 at 22:50