Hello there, I'm currently encountering a strange error which is I don't know why or could this one be possible. I have several for loops inside a function and I used the iterator outside the said loop, but I do get this error which is kinda strange.

error: name lookup of 'i' changed for new ISO 'for' scoping
error: using obsolete binding at 'i'

Strange.

You are trying to do something like this:

for (int i = 0; i < 12; i++) {
  ...
  }
if (i < 12) ...

The variable i is only supposed to exist inside the for loop. The compiler is complaining about your using it outside the loop.

Comments
Exactly

Yep, that is exactly what the code is doing. I'm actually porting a windows code to linux. And I was wondering why did he(windows programmer) did that.

A lot of older compilers, particularly windows compilers, let you get away with a lot of stuff.

Before local scoping was put in the standard, what do compilers do with something like for (int i = ... ?

The most flexible answer was simply to pretend that the variable was declared at the beginning of the function.

So, to fix the code, just put int i; at the beginning of the function and change to loop to for (i = ... .

Enjoy... ;)

I was wondering why the windows compiler (specifically win32) tolerated such kind of error. It'll surely work. Thanks!

> I was wondering why the windows compiler (specifically win32) tolerated such kind of error
past tense is correct; it does not any more.
microsoft c++ 6 was released prior to the the adoption of c++98; and behaved like c as far as for loops were concerned.
microsoft c++ 7 ( released 2003) provided c++98 conformance for this; but the default was backward compatibility with the earlier version. conformance had to be turned on explicitly ( /Zc compiler switch ).
microsoft c++ 8 ( released 2005) made c++98 conformance the default; but you can explicitly turn it off (again, with the /Zc compiler switch ).
typical microsoft; they haven't got to where they are without being pragmatic. microsoft's implementation of the c++ compiler is very good. stroustrup gave a talk on c++0x at the computer science club of waterloo university a couple of months back. here is an interesting extract from the question and answer session which followed:

Q: What’s your opinion about the Microsoft implementation of C++?
A: Microsoft’s implementation is the the best out there, they conform to the standards pretty well and the code generated is also good. GNU gcc is also good. Though, they want you to use their “Managed C++” called C++/CLI which is totally unportable. Apple does the same with their version of C++ which is Objective C/C++ and and so does GNU. They all play this game of trying to get users just to use their product and not switch to their competitors products.

the entire lecture can be downloaded from http://csclub.uwaterloo.ca/media/C++0x%20-%20An%20Overview.html

This article has been dead for over six months. Start a new discussion instead.