954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Help with error in using iterator in a for loop

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.

jaepi
Practically a Master Poster
647 posts since Jul 2006
Reputation Points: 32
Solved Threads: 4
 

code would be helpful.

vmanes
Posting Virtuoso
1,914 posts since Aug 2007
Reputation Points: 1,268
Solved Threads: 228
 

You are trying to do something like this:

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

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

Duoas
Postaholic
2,043 posts since Oct 2007
Reputation Points: 1,140
Solved Threads: 229
 

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.

jaepi
Practically a Master Poster
647 posts since Jul 2006
Reputation Points: 32
Solved Threads: 4
 

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... ;)

Duoas
Postaholic
2,043 posts since Oct 2007
Reputation Points: 1,140
Solved Threads: 229
 

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

jaepi
Practically a Master Poster
647 posts since Jul 2006
Reputation Points: 32
Solved Threads: 4
 

> 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

vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You