Skip to main content
GameDev.net gamedev.net
🔒 Locked

C++ scope rules

Started by CaptainJester Jan 20, 2002 at 9:22 PM 7 replies 1.9k views
Original Post
CaptainJester
CaptainJester
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:
  
for(int i=0;i<12;i++) {
	temp=buffer[i]*32;
	source.top=(temp/512)*32;
	source.left=(temp%32)*32;
	source.top=((temp/512)+1)*32;
	source.left=((temp%32)+1)*32;
	dest.top=baseY;
	dest.left=baseX+(i*32);
	dest.bottom=baseY+32;
	dest.right=baseX+((i+1)*32);
	lpddsback->Blt(&dest,lpddsmenufont,&source,DDBLT_WAIT,NULL);
}
baseY+=32;
strcpy(buffer,"Campaign\0");
for(int i=0;i<8;i++) {  // <<== compiler error occurs here

	temp=buffer[i]*32;
	source.top=(temp/512)*32;
	source.left=(temp%32)*32;
	source.top=((temp/512)+1)*32;
	source.left=((temp%32)+1)*32;
	dest.top=baseY;
	dest.left=baseX+(i*32);
	dest.bottom=baseY+32;
	dest.right=baseX+((i+1)*32);
	lpddsback->Blt(&dest,lpddsmenufont,&source,DDBLT_WAIT,NULL);
}
  
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
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][
BaShildy
BaShildy
Your code is legal C++.
  
{
int j;
for (int i = 0, parameter2, parameter3)
{
k = 0;
}
}

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
Kevin "BaShildy" KingGame Programmer: DigiPenwww.mpogd.com
pizza box
pizza box
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
CaptainJester
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
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][
Oluseyi
Oluseyi
quote:
Original post by CaptainJester
My code should be legal C++ because ''i'' is only defined within the scope of the for loop.

quote:
Original post by BaShildy
Your code is legal C++.

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
Dredge-Master
you can do this and it will work under C++.

  
for(int i=0;i<3;i++)
{
printf("%i\n",i);
for(int i=0;i<3;i++)
printf(" |%i\n",i);
}


No problems should arise AT ALL if your C++ compiler is working properly.

output should be
  
0
|0
|1
|2
1
|0
|1
|2
2
|0
|1
|2



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
Beer - the love catalystgood ol' homepage
MonkeyChuff
MonkeyChuff
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
CaptainJester
Thank''s for all your help everyone.

---
Make it work.
Make it fast.

"Commmmpuuuuterrrr.." --Scotty Star Trek IV:The Voyage Home
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.