title: Problems when looping through a list of pointers - C++
body: <p>I have a list of pointers to a base abstract class (Entity)</p>
  
  <pre><code>std::list&lt;Entity*&gt; m_entities;
  </code></pre>
  
  <p>I have created a typedef for iterating through this class</p>
  
  <pre><code>typedef std::list&lt;Entity*&gt;::const_iterator entityIter;
  </code></pre>
  
  <p>I then try and iterate through each pointer in the list</p>
  
  <pre><code>for (entityIter i = m_entities.begin(); i != m_entities.end(); ++i)
  {
      const Entity &amp;e = *i;    // ERROR
      e.DoStuff();
  }
  </code></pre>
  
  <p>I get the following error when attempting to reference each pointer</p>
  
  <blockquote>
    <p>IntelliSense: no suitable constructor exists to convert from "Entity *const" to "Entity" </p>
  </blockquote>
  
  <p>What have I done incorrectly?</p>
  
  <p>EDIT:</p>
  
  <p>I have tried to use std::shared_ptr</p>
  
  <pre><code>std::list&lt;std::shared_ptr&lt;Entity&gt;&gt; m_entities;
  </code></pre>
  
  <p>I can't add to the list this way though</p>
  
  <pre><code>Entity::Entity(Game *game) 
      : m_game(game)                  
  {
      m_game-&gt;g_idGenerator-&gt;generateNewID();
  
      m_game-&gt;m_entities.push_back(this);      // ERROR
  }
  </code></pre>
  
  <p>Using the following</p>
  
  <pre><code>m_game-&gt;m_entities.push_back(std::shared_ptr&lt;Entity&gt;(this));
  </code></pre>
  
  <p>gives me this error</p>
  
  <blockquote>
    <p>error C2664: 'void std::list&lt;_Ty>::push_back(_Ty &amp;&amp;)' : cannot convert parameter 1 from >'Entity' to 'std::tr1::shared_ptr&lt;_Ty> &amp;&amp;'</p>
  </blockquote>
  
  <p>EDIT 2:</p>
  
  <h2>Current code summary</h2>
  
  <pre><code>for (entityIter i = m_entities.begin(); i != m_entities.end(); ++i)
  {
      // *i dereferences the iterator and returns an Entity*
      // **i would additionally deference the pointer
      // Adding 'const' infront of Entity means that I can't alter the Entity
      Entity &amp;e = **i;
  
      e.draw(dt);   // Causes access violation error with standard pointers
  }   
  </code></pre>
  
  <p>Have tried converting to std:shared_ptr to see if it would avoid the error triggered by the code above.  </p>
  
  <p>However,  I am now having trouble adding the Entity to the list of std::shared_ptr</p>
  
  <pre><code>    m_game-&gt;m_entities.push_back(std::shared_ptr&lt;Entity&gt;(this));
  </code></pre>
  
  <p>So in summary I have the access violation error with a standard pointer and I can't add to the list with a shared_ptr.</p>
  
  <p>Populating the list is done via the constructor of the base Entity class</p>
  
  <pre><code>Entity::Entity(Game *game) 
      : m_game(game)                  
  {
      m_game-&gt;g_idGenerator-&gt;generateNewID();
  
              // shared_ptr version
      m_game-&gt;m_entities.push_back(std::shared_ptr&lt;Entity&gt;(this));  // ERROR C2664
  
              // raw pointer version
              //m_game-&gt;m_entities.push_back(this);  // ACCESS VIOLATION ERROR when calling methods
  }
  </code></pre>