|
||||||||||||||||||
Add Forum to Favorites | Send Topic To a Friend | View Forum FAQ | Track this topic |
Last Thread Next Thread ![]() |
| C++ scope rules |
|
![]() CaptainJester Member since: 11/29/2001 From: Ottawa, Canada |
||||
|
|
||||
From what I have read, the following code should compile correctly. Since 'i' is declared within the scope of the for loop, it should go out of scope as soon as the for loop is done. Here is the code:
Now when I compile this code, I get the following error: C:\Program Files\Microsoft Visual Studio\MyProjects\AmazingTank\Graphics.cpp(185) : error C2374: 'i' : redefinition; multiple initialization C:\Program Files\Microsoft Visual Studio\MyProjects\AmazingTank\Graphics.cpp(171) : see declaration of 'i' I know when I do this in Java, this will compile correctly. I was under the impression that it is supposed to be legal in C++ as well. Am I wrong? --- Make it work. Make it fast. "Commmmpuuuuterrrr.." --Scotty Star Trek IV:The Voyage Home |
||||
|
||||
![]() BaShildy Member since: 10/31/2001 From: Redmond, USA |
||||
|
|
||||
Your code is legal C++.
In ANSI C++, k and i are in the same scope. In Visual C++'s compiler, j and i are in the same scope. Basically declare the int outside of the for loop, and reference it in the parameter 1, but not declare it. Someone here came up with a macro to make for loops have an extra {} (epoch), but i forgot where it is. Hope that helped. - Kevin "BaShildy" King Game Programmer: DigiPen www.mpogd.com Edited by - BaShildy on January 20, 2002 10:31:12 PM |
||||
|
||||
![]() pizza box Member since: 10/25/2001 From: Methuen, MA, United States |
||||
|
|
||||
| I believe your code is illegal because you define i twice with integer types. In the first loop you can define i as an integer and set it to 0, but in the second you loop you just set i back to 0. Or another way to do it is to define i as an integer in the beginning: int i; And then in the each loop just set it to 0: for ( i = 0; i < 12; i++ ) I am not entirely sure if this is the problem, but you can try it anyways. |
||||
|
||||
![]() CaptainJester Member since: 11/29/2001 From: Ottawa, Canada |
||||
|
|
||||
| But according to object oriented rules, anything that is defined within a scope, be comes undefined at the end of that scope, so is available to be reused. My code should be legal C++ because 'i' is only defined within the scope of the for loop. --- Make it work. Make it fast. "Commmmpuuuuterrrr.." --Scotty Star Trek IV:The Voyage Home |
||||
|
||||
![]() Oluseyi Staff Member since: 5/14/2001 From: New York, NY, United States |
||||
|
|
||||
quote: quote: This is a known conformance bug in MSVC, supposedly left in because of legacy client code. Here's a quick fix (until VC.NET [7.1]): #define for if(0) {} else for Enjoy! [ GDNet Start Here | GDNet FAQ | MS RTFM | STL | Google ] Thanks to Kylotan for the idea! |
||||
|
||||
![]() Dredge-Master Member since: 12/13/2001 From: Australia |
||||
|
|
||||
you can do this and it will work under C++.
No problems should arise AT ALL if your C++ compiler is working properly. output should be
The scope of a variable lasts in its own level and its children unless its name is reused, it is ignored till the reused name is complete. Beer - the love catalyst good ol' homepage |
||||
|
||||
![]() Dredge-Master Member since: 12/13/2001 From: Australia |
||||
|
|
||||
| ps - dont forget to include stdio.h and make the main function, or you will get errors obviously. Beer - the love catalyst good ol' homepage |
||||
|
||||
![]() MonkeyChuff Member since: 8/30/2001 From: London, UK |
||||
|
|
||||
| From VC++ 6.0 Project ... Settings ... C/C++ Tab ... Category = Customize Check the Disable Language Extensions box Your code will now work as expected. The scope of i will be within the loop. You will lose *ALL* of the Microsoft extensions to the language by doing this, although some may say that is a good thing |
||||
|
||||
![]() CaptainJester Member since: 11/29/2001 From: Ottawa, Canada |
||||
|
|
||||
| Thank's for all your help everyone. --- Make it work. Make it fast. "Commmmpuuuuterrrr.." --Scotty Star Trek IV:The Voyage Home |
||||
|
||||
All times are ET (US)![]() |
Last Thread Next Thread ![]() |
|