Home » Community » Forums » General Programming » C++ scope rules
  Intel sponsors gamedev.net search:   
[Control Panel] [Register] [Bookmarks] [Who's Online] [Active Topics] [Stats] [FAQ] [Search]

Add Forum to Favorites |  Send Topic To a Friend | View Forum FAQ | Track this topic


 Last Thread Next Thread 
 C++ scope rules
Post New Topic  Post Reply 
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

 User Rating: 1279   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

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

 User Rating: 1015   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

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.

 User Rating: 1045   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

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

 User Rating: 1279   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

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!


 User Rating: 2027   |  Rate This User  Send Private MessageView ProfileView Journal Report this Post to a Moderator | Link

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

 User Rating: 1050   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

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

 User Rating: 1050   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

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 .

 User Rating: 1038   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

Thank's for all your help everyone.

---
Make it work.
Make it fast.

"Commmmpuuuuterrrr.." --Scotty Star Trek IV:The Voyage Home

 User Rating: 1279   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

All times are ET (US)

Post Reply
 Last Thread Next Thread 
Forum Rules:
You may not post new threads
You may post replies
You may not edit your posts
You may not use HTML in your posts
Jump To:
Administrative Options: