Hey there! I'm doing this project and right now I'm trying to:
- create some of objects and store them in vectors, which get stored in another vector V
- iterate through the vectors inside V
- iterate through the objects inside the individual vectors
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:
for_each(V.begin(), V.end(), iterateThroughSmallVectors);
the iterateThroug.... simply does the same on the vector passed to it..
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..
I don't know if it helps, but V is a private vector<> stored in class A, which has an accessor to it, and I'm trying to iterate through it in class B by doing:
A->getV().begin(), A->getV().end(), etc..
Anyone got any idea of what is going on?
EDIT: Ok, so I think it is better to just post the code, and where problems might be arrising...
getTiles in gameState.h:
vector<vector<tile*>> getTiles();
for_each loops in main.cpp:
for_each(currState->getTiles().begin(),currState->getTiles().end(), drawTiles);
.
.
void drawTiles(vector<tile*> row)
{
for_each(row.begin(), row.end(), dTile);
}
void dTile(tile *t)
{
t->draw();
}
creating the vectors:
int tp = -1;
int bCounter = 0;
int wCounter = 0;
for (int i = 0; i < 8; i++)
{
vector<tile*> row(8);
for (int j = 0; j < 8; j++)
{
tile *t = new tile(tp, (i+(SIDELENGTH/2))*SIDELENGTH,
(j+(SIDELENGTH/2))*SIDELENGTH);
row.push_back(t);
tp *= -1;
}
currState->setTiles(row);
tp *= -1;
}
and just in case it might be relevant:
void gameState::setTiles(vector<tile*> val)
{
tiles.push_back(val);
}
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.
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 :
for (int j = 0; j < 8; j++)
{
tile *t = new tile(tp, (i+(SIDELENGTH/2))*SIDELENGTH,
(j+(SIDELENGTH/2))*SIDELENGTH);
row.push_back(t);
tp *= -1;
}
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