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

Variable Scope, anyone?

Started by Ferrit Sep 30, 2000 at 9:22 AM 4 replies 250+ views
Original Post
Ferrit
Ferrit
Ok, ok, before I begin, I must tell you something... I use Borland C++ Compilers. Why? Well, the obselete versions are free, as i''m to broke to afford £49.99 for Visual C++. Anyway... When I was using Borland C++ 5.5, I could do the following...
    
for(int ctr=0; ctr<100; ctr++)
{
   ...Render some stuff...
}
[/source]

and everything was fine. Now, I re-installed windows due to some
problems, and resorted to using Borland C++ 4.52. But! If I do
this:

[source]
for(int ctr=0; ctr<100; ctr++)
{
   ...Render some stuff...
}

for(int ctr=0; ctr<100; ctr++)
{
   ...Do something nice...

    
I get the error "multiple declarations for ''ctr''" which annoys me most. I know I could just do int ctr, but when I''m doing loads of sub-''for''s, it gets irritationg declaring ctr2...ctr15, especially when I''m coding on-the-fly, trying out new ideas, as sometimes I would use ctr instead of, say, ctr2. This causes incorrects results in my functions, and takes me a while to work this out. So... is this a Borland C++ 4.52 thing, or something else? Thanks to all who reply
JonStelly
JonStelly
Take a close look at where you're declaring your 'ctr' variable. Parenthesis don't mean anything for variable scope, only brackets. Since you're declaring it outside of the for brackets, it is visible in the scope directly above this code, for the entire section.

The following would work:


for(int i=0; i < 100; i++)
{
//Do_Work
}
for(i=0; i < 200; i++)
{
//Do_More_Work
}



Hope that helps,
Jon

Edited by - JonStelly on September 30, 2000 12:41:38 PM
3dModelMan
3dModelMan
That isn''t down to Borland, it is a C++ convention.

You''ll find that compiler will let you get away with multiple definitions like this;

    
for (int a = 0; a < 10; a++)
{
for (int b = 0; b < 10; b++)
//nested loop code here...

}



but you can''t repeat a definition like you are trying to do.

My advice would be to try and be a bit more creative with your variable names!

Later

Matt




Check out my project at:www.btinternet.com/~Matthew.Bennett
Gorg
Gorg
This is an implementation problem. Visual C++ has the same problem. They are not following the standard correctly.(Pisses me off!)

Just declare your variable at the top of the for-loops and don''t declare them in it.

For those who weren''t aware, the C++ standard states that if you declare a variable in the for-loop declaration

for( int a, int b ; ...)

a and b will for scope the for-loop, but Visual C++ and older version of the Borland compiler does not follow the standard. I guess it is because they want to be backward compatible with C and unstandardize C++ versions whick don''t have that.
JonStelly
JonStelly
Gorg, you''re right. I had grown so accustomed to it that I had forgotten that it wasn''t part of the spec.

so the way you would probably want to do this is:



int i;
for(i=0; i < 100; i++)
{
...
}
for(i=0; i < 200; i++)
{
...
}



GKW
GKW
I have not used borland in a while but I seem to remember that there is a compiler setting for for-loop variable scope. You should be able to keep your original code but the previous post would be a better way of doing it.

I wanrned you! Didn't I warn you?! That colored chalk was forged by Lucifer himself!
"... we should have such an empire for liberty as she has never surveyed since the creation ..."Thomas Jefferson

Topic Locked

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

Sign in to reply to this topic.