"J is not declared"

when trying to loop through a vector, i come across an error.

1
2
3
4
5
6
7
for (unsigned int i = 0; i != width_; i++) {
            for(unsigned int j = 0; j != height_; j++)
                rtrn.all[i][j].setTexture(Barrier);
                rtrn.all[i][j].setPosition(Vector2f(i * scl, j * scl));
                rtrn.all[i][j].setTextureRect(getRect(false, true, false, 1, 0));
         //       cout << endl << "K" << endl << int(index / width_) * scl << endl << (index - (int((index) / width_) * width_)) * scl << endl;
    }

and the interesting thing is that it gives me an error at
 
rtrn.all[i][j].setPosition(Vector2f(i * scl, j * scl));
Last edited on
Hi,

You need braces for the inner loop. It's good practise to put braces - even if there is only 1 line for the body of the loop :+)
Lets adjust whitespace (which does not change the code in any way):
1
2
3
4
5
6
7
8
9
for (unsigned int i = 0; i != width_; i++)
{
  for(unsigned int j = 0; j != height_; j++)
    rtrn.all[i][j].setTexture(Barrier);

  rtrn.all[i][j].setPosition(Vector2f(i * scl, j * scl));
  rtrn.all[i][j].setTextureRect(getRect(false, true, false, 1, 0));
  // cout << endl << "K" << endl << int(index / width_) * scl << endl << (index - (int((index) / width_) * width_)) * scl << endl;
}

and then add the optional braces (again, code does not change at all):
1
2
3
4
5
6
7
8
9
10
11
for (unsigned int i = 0; i != width_; i++)
{
  for(unsigned int j = 0; j != height_; j++)
  {
    rtrn.all[i][j].setTexture(Barrier);
  }

  rtrn.all[i][j].setPosition(Vector2f(i * scl, j * scl));
  rtrn.all[i][j].setTextureRect(getRect(false, true, false, 1, 0));
  // cout << endl << "K" << endl << int(index / width_) * scl << endl << (index - (int((index) / width_) * width_)) * scl << endl;
}

Does anything start to look odd now?
Oops sorry, I'm kind of an idiot for not seeing that, I was so used to already have the braces that I totally forgot about them!

Thanks, It's always useful to have another pair of eyes on something!
Topic archived. No new replies allowed.