0

just a heads up - i'm new to C++. I can't seem to make 2 objects reference each other. I've read about forward declarations and tried unique_ptr() as well as raw pointers, but i just can't get it to work... Some help would be really appreciated

So here's the idea (simplified): I have a Renderer class

class EventHandler;

class Renderer {
    EventHandler *eventHandler;
public:
    void addDrawing(sf::Drawable *);
};

and an EventHandler class

class Renderer;

class EventHandler {
    Renderer *renderer;
};

Basically, i create Renderer which uses EventHandler. EventHandler then should also be able to use Renderer (call addDrawing method), but it should use the one and only instance of that Renderer. (this is important).

So currently there are 2 problems:

  • when eventHandler calls addDrawing the program crashes, i'm not sure why, but my guess is that the Renderer object is not yet fully initialised, even though an address exists for it.
  • when i create Renderer in EventHandler it is not the original Renderer instance, but a pointer to a new instance, as far as i understand. I need the original one.

And i know how to make all this work, by simply passing the Renderer as parameter, but i want it on a global scale, not local in EventHandler.

  • To _reference_ something use `&` references not pointers. –  Jan 28 '18 at 17:11
  • 2
    You should use a debugger instead of guessing. Also not sure we can help, This is far from a minimal example. You simplified your code way too much. – drescherjm Jan 28 '18 at 17:11
  • Possible duplicate of [Resolve build errors due to circular dependency amongst classes](https://stackoverflow.com/questions/625799/resolve-build-errors-due-to-circular-dependency-amongst-classes) –  Jan 28 '18 at 17:12
  • 3
    Since the program crashes the problem is not resolving build errors. – drescherjm Jan 28 '18 at 17:13
  • There is no way to make `EventHandler::renderer` to point anything because it is private and there are no member functions. – eerorika Jan 28 '18 at 17:13
  • thanks @TheDude https://stackoverflow.com/a/625801/7779823 this was very helpful, finally made it work. – Povilas Gintutis Jan 28 '18 at 18:04

0 Answers0