title: C++ - How to use a vector of reference_wrapper
body: <p>I'm trying to refactor part of a pathfinding algorithm I had that used pointers to not use pointers. Unfortunately I'm not that knowledgable about references. I get the error: <code>Invalid operands to binary expression ('std::__1::reference_wrapper&lt;Tile&gt;' and 'const std::__1::reference_wrapper&lt;Tile&gt;')</code></p>
  
  <p>I also have no idea what that means. My code is below, and I can gather that it comes from the line: <code>openList.erase(std::find(openList.begin(), openList.end(), current));</code> but I'm not sure how to fix this.</p>
  
  <pre><code>bool TileMap::tilesBetween(Tile&amp; p_start, Tile&amp; p_end)
  {
      std::vector&lt;std::reference_wrapper&lt;Tile&gt;&gt; openList;
      std::vector&lt;std::reference_wrapper&lt;Tile&gt;&gt; closedList;
  
      openList.push_back(p_start);
  
      do
      {
          std::sort(openList.begin(), openList.end(), sortF());
          Tile&amp; current = openList[0];
          closedList.push_back(current);
          openList.erase(std::find(openList.begin(), openList.end(), current));
          if(std::find(closedList.begin(), closedList.end(), p_end) != closedList.end())
          {
              return true;
          }
  
          std::vector&lt;std::reference_wrapper&lt;Tile&gt;&gt; adjacentTiles;
          if (current.m_coordinates.x &gt; 0)
          {
              adjacentTiles.push_back(m_tiles[current.m_coordinates.y * m_width + (current.m_coordinates.x - 1)]);
          }
          if (current.m_coordinates.x &lt; m_width)
          {
              adjacentTiles.push_back(m_tiles[current.m_coordinates.y * m_width + (current.m_coordinates.x + 1)]);
          }
          if (current.m_coordinates.y &gt; 0)
          {
              adjacentTiles.push_back(m_tiles[(current.m_coordinates.y - 1) * m_width + current.m_coordinates.x]);
          }
          if (current.m_coordinates.y &lt; m_height)
          {
              adjacentTiles.push_back(m_tiles[(current.m_coordinates.y + 1) * m_width + current.m_coordinates.x]);
          }
  
          for(auto t : adjacentTiles)
          {
              if(std::find(closedList.begin(), closedList.end(), t) != closedList.end())
              {
                  continue;
              }
  
              if(std::find(openList.begin(), openList.end(), t) == closedList.end())
              {
                  openList.push_back(t);
              }
          }
      }
      while(!openList.empty());
  
      return false;
  }
  </code></pre>
  
  <p>EDIT: posted <code>sortF</code></p>
  
  <pre><code>struct sortF
  {
      bool operator()(const Tile* p_a, const Tile* p_b) const
      {
          return p_a-&gt;f &lt; p_b-&gt;f;
      }
  };
  </code></pre>
  
  <p>UPDATE: As per the suggestion, I've changed the function to use pointers instead of references. It SEEMS to be working, but I have more to implement before it's finished.</p>
  
  <pre><code>bool TileMap::tilesBetween(Tile* p_start, Tile* p_end)
  {
      std::vector&lt;Tile*&gt; openList;
      std::vector&lt;Tile*&gt; closedList;
  
      std::cout &lt;&lt; p_start &lt;&lt; ", ";
  
      openList.push_back(p_start);
  
      do
      {
          std::sort(openList.begin(), openList.end(), sortF());
          Tile* current = openList[0];
          closedList.push_back(current);
          openList.erase(std::find(openList.begin(), openList.end(), current));
          if(std::find(closedList.begin(), closedList.end(), p_end) != closedList.end())
          {
              return true;
          }
  
          std::vector&lt;Tile*&gt; adjacentTiles;
          if (current-&gt;m_coordinates.x &gt; 0)
          {
              adjacentTiles.push_back(&amp;m_tiles[current-&gt;m_coordinates.y * m_width + (current-&gt;m_coordinates.x - 1)]);
          }
          if (current-&gt;m_coordinates.x &lt; m_width)
          {
              std::cout &lt;&lt; &amp;m_tiles[current-&gt;m_coordinates.y * m_width + (current-&gt;m_coordinates.x + 1)] &lt;&lt; std::endl;
              adjacentTiles.push_back(&amp;m_tiles[current-&gt;m_coordinates.y * m_width + (current-&gt;m_coordinates.x + 1)]);
          }
          if (current-&gt;m_coordinates.y &gt; 0)
          {
              adjacentTiles.push_back(&amp;m_tiles[(current-&gt;m_coordinates.y - 1) * m_width + current-&gt;m_coordinates.x]);
          }
          if (current-&gt;m_coordinates.y &lt; m_height)
          {
              adjacentTiles.push_back(&amp;m_tiles[(current-&gt;m_coordinates.y + 1) * m_width + current-&gt;m_coordinates.x]);
          }
  
          for(auto t : adjacentTiles)
          {
              if(std::find(closedList.begin(), closedList.end(), t) != closedList.end())
              {
                  continue;
              }
  
              if(std::find(openList.begin(), openList.end(), t) == openList.end())
              {
                  openList.push_back(t);
              }
          }
      }
      while(!openList.empty());
  
      return false;
  }
  </code></pre>