-3

I got a code of snake in c++ sfml.. I got 2 objects here Snake and Food, also I have a main() that contains sfml setup for windows. I would simply make 3 objects that are called from main but it results in circular references and can't compile.

What I want to have is (or something close to the idea below).

int main
{ 
    Food food(window); 
    Snake snake(window, food); 
    Window window(snake, food); 

    window.start(); 
}

snake gas method draw that needs reference to its window (for drawing as drawing needs window handle), and update whuch also needs window (for window size)

food also needs those for close reason (need draw and window suze)

window in turn has its window loop from where it need to calls those updates and draws of snake and food

//edit

someone asked for definitions but definitions are strightforward

    class Snake;
    class Food;

    class Window
    {
    public:
      Window::Window(Snake& s, Food& f): snake(s), food(f)
      {

      }
    private:
      Snake& snake;
      Food& food;

    }

    class Snake
    {
    public:
      Snake::Snake(Window& w, Food& f): window(w), food(f)
      {

      }
    private:
      Window& window;
      Food& food;

    }

    class Food
    {
    public:
      Snake::Snake(Window& w): window(w)
      {

      }
    private:
      Window& window;

    }
J. Stone
  • 77
  • 10
  • Use some sort of references or pointers. Don't directly contain objects, your code will compile fine. May need an extra bit of ownership management, but that's easy with `shared_ptr` now. – Tanveer Badar Feb 04 '20 at 18:22
  • 1
    related/dupe: https://stackoverflow.com/questions/625799/resolve-build-errors-due-to-circular-dependency-amongst-classes – NathanOliver Feb 04 '20 at 18:22
  • if its related where is the answer to what i state? – J. Stone Feb 04 '20 at 18:24
  • no it seem to be about headers and i dont get a problem with headers but with references – J. Stone Feb 04 '20 at 18:28
  • 2
    Please post a [mcve] that we can comment on. – Jesper Juhl Feb 04 '20 at 18:29
  • its posted above its real code i wand, in constructors you simply assign references to internal fields... note i dont want to use pointers unless realy nothing else work... if i need pointers i would ask if someone would show me how (im experienced c coder but c++ newb) – J. Stone Feb 04 '20 at 18:31
  • definitions are strightforward, i may add them bit you may imagine them – J. Stone Feb 04 '20 at 18:37
  • It would seem that people are having trouble understanding what is being asked here. I'm reading that code "cant compile", but the code shown can compile. If you are asking why code can't compile, please show code that we can attempt to compile. – Drew Dormann Feb 04 '20 at 18:41

1 Answers1

1

how to compile c++ code that has circular references?

You cannot compile C++ code that has circular references.

If you have a circular reference, the solution is to break the cycle, so that dependency graph becomes a directed acyclic graph.

There is another form if indirection in C++ besides references: pointers. Because pointers can be default initialised and assigned later, they can form cyclical structures.

A pattern that can break cyclical initialisation is two phase initialisation: First create Food and Snake with no association to a Window, and only after Window is created, call a function that performs the second phase of initialisation. Two phase initialisation cannot be used with references, but can be used with pointers.

eerorika
  • 232,697
  • 12
  • 197
  • 326