title: C++: Iterating through a vector of vectors
body: <p>Hey there! I'm doing this project and right now I'm trying to:</p>
  
  <ol>
  <li>create some of objects and store them in vectors, which get stored in another vector V</li>
  <li>iterate through the vectors inside V</li>
  <li>iterate through the objects inside the individual vectors</li>
  </ol>
  
  <p>Anyway, I was just searching the web and I came accross the stl for_each function. It seems pretty neat but I'm having problems with it. I'm trying to use it in this way:</p>
  
  <pre><code>for_each(V.begin(), V.end(), iterateThroughSmallVectors);
  </code></pre>
  
  <p>the iterateThroug.... simply does the same on the vector passed to it..</p>
  
  <p>Now I'm getting a weird "Vector iterators incompatible" runtime error. I've looked on it and can't find any useful input on this..</p>
  
  <p>I don't know if it helps, but V is a private vector&lt;> stored in class A, which has an accessor to it, and I'm trying to iterate through it in class B by doing:</p>
  
  <pre><code>A-&gt;getV().begin(), A-&gt;getV().end(), etc..
  </code></pre>
  
  <p>Anyone got any idea of what is going on?</p>
  
  <p>EDIT: Ok, so I think it is better to just post the code, and where problems might be arrising...</p>
  
  <p>getTiles in gameState.h:</p>
  
  <pre><code>vector&lt;vector&lt;tile*&gt;&gt; getTiles();
  </code></pre>
  
  <p>for_each loops in main.cpp:</p>
  
  <pre><code>for_each(currState-&gt;getTiles().begin(),currState-&gt;getTiles().end(), drawTiles);
  .
  .
  void drawTiles(vector&lt;tile*&gt; row)
  {
  for_each(row.begin(), row.end(), dTile);
  }
  void dTile(tile *t)
  {
  t-&gt;draw();
  }        
  </code></pre>
  
  <p>creating the vectors:</p>
  
  <pre><code>int tp = -1;
  int bCounter = 0;
  int wCounter = 0;
  for (int i = 0; i &lt; 8; i++)
  {
  vector&lt;tile*&gt; row(8);
      for (int j = 0; j &lt; 8; j++)
      {
      tile *t = new tile(tp, (i+(SIDELENGTH/2))*SIDELENGTH,
          (j+(SIDELENGTH/2))*SIDELENGTH);
      row.push_back(t);
              tp *= -1;
      }
  currState-&gt;setTiles(row);
      tp *= -1;
  }
  </code></pre>
  
  <p>and just in case it might be relevant:</p>
  
  <pre><code>void gameState::setTiles(vector&lt;tile*&gt; val)
  {
      tiles.push_back(val);
  }
  </code></pre>
  
  <p>Is it easier to spot the problem now? I hope so... And if you do spot any stupid stuff I might be doing, please let me know, I'm kind of new to C++ and the pointers and references still confuse me.</p>
  
  <p>EDIT2: Thanks guys, that worked perfectly... well for that problem, now it seems I have an issue with the creation of the tiles and stroing them in the row vector.. it seems that even through the vector is created and passes correctly, the tiles that were supposed to be in it aren't (they are lost after the :</p>
  
  <pre><code>    for (int j = 0; j &lt; 8; j++)
      {
      tile *t = new tile(tp, (i+(SIDELENGTH/2))*SIDELENGTH,
          (j+(SIDELENGTH/2))*SIDELENGTH);
      row.push_back(t);
              tp *= -1;
      }
  </code></pre>
  
  <p>loop. If any of you has any good ideas about solving this you're welcome to help me ;) In the mean time, I'll keep trying to fix it</p>