You are currently offline, waiting for your internet to reconnect

PRB: Variable Scope in for-statement Extends Beyond Loop

This article has been archived. It is offered "as is" and will no longer be updated.
If you declare the same variable in two different for-statements, thefollowing error may appear:
Test.cpp(4) : error C2374: 'i' : redefinition; multiple initialization
The scope of a variable declared in the initialization part of a "for" loopis given local scope, as if it had been declared immediately prior to thefor-statement.
Compile with /Za, Disable Language Extensions. To select this option inDeveloper Studio, select Project, Settings, and then click the C/C++ Tab.Change the Category to Customize and select Disable Language Extensions.

If you cannot use the /Za option, you can work around the problem by #defining the "for" keyword as follows:
#define for if(0);else for				
More information
/* Compile Options Needed to compile without error: /Za */  /* test.cpp */  void main()  {for (int i=0; i<10; i++)   ;for (int i=0; i<15; i++)   ; }				

Article ID: 167748 - Last Review: 01/09/2015 06:45:58 - Revision: 2.0

  • kbnosurvey kbarchive kblangcpp kbprb KB167748