title: How to have a class contain a list of pointers to itself?
body: <p>I'm making a game in C++. In this game, there is a list of GameObject pointers. GameObject is the base class for every object one may interact with in the world. Main's job is to act as the referee, so it informs each object about collisions, renders them, deletes them, etc.</p>
  
  <p>The player's character has abilities. Some abilities may spawn projectiles. Each projectile is different, but all derive from GameObject. They must be added to the list so main may see them. I want the list of GameObject pointers to be global, so that objects may push themselves onto the list, but main may still see and update them. As it stands, the game runs if the list is created in main.</p>
  
  <p>There are 3 possible solutions: I put this list in Globals.h, I apply singleton pattern to the GameObject class, or I put this list in GameObject.h, but outside of the GameObject class. All solutions result in errors.</p>
  
  <p>I believe solution 1 results in a circular dependency, as Globals.h defines enumerated values needed by the GameObject class, but the list is made up of GameObject pointers. The first error on the list says "GameObject is undeclared identifier", and crops up in Globals.</p>
  
  <pre><code>//Globals.h
  #include "GameObject.h"
  #include &lt;list&gt;
  std::list&lt;GameObject *&gt; objects;
  enum Id{ENEMY, PLAYER, ROCK};
  
  //GameObject.h
  #pragma once
  #include "Globals.h"
  class GameObject{
      public:
          GameObject();
          ~GameObject();
          Id id;
  };
  </code></pre>
  
  <p>For solution 2, I am able to compile if I have the list inside the GameObject class, but as soon as I try to push itself onto the list, the linker throws errors. It says "unresolved external symbol". No changes if objects itself is a pointer either.</p>
  
  <pre><code>//GameObject.h
  #pragma once
  #include "Globals.h"
  #include &lt;list&gt;
  class GameObject{
      public:
          static std::list &lt;GameObject *&gt; objects;
          GameObject();
          ~GameObject();
          void init(){ objects.push_back(this); }
          Id id;
  };
  </code></pre>
  
  <p>For the final solution, the linker tells me that main.obj has already defined list  objects symbol. One or more multiply symbols found.</p>
  
  <pre><code>//GameObject.h
  #pragma once
  #include "Globals.h"
  #include &lt;list&gt;
  class GameObject{
      public:
          GameObject();
          ~GameObject();
          Id id;
  };
  std::list &lt;GameObject *&gt; objects;
  </code></pre>
  
  <p>I have completely run out of ideas, and I really need this list to be accessible. Solution 1 is most preferable, followed by 2 and then 3. How can I make this list globally accessible? Is it possible? Is there a better way than the 3 ways I've tried?</p>
  
  <p>Update: Solution 1 will compile if I use forward declaring and the extern keyword, but when I try</p>
  
  <pre><code>GameObject *test = new GameObject
  objects.push_back(test);
  </code></pre>
  
  <p>in main, I get an unresolved external.</p>