Click here to Skip to main content
15,867,834 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Below is my procedure ,I don't think the 'i' is redefine,but there is an error says:E:\c++test\test\a.cpp(5) : error C2374: 'i' : redefinition; multiple initialization
E:\c++test\test\a.cpp(4) : see declaration of 'i'
can anybody help me? tell me why?

C++
#include <iostream>
void main()
{
	for(int i=0;i<10;i++);
	for(int i=0;i<10;i++);

}
Posted
Comments
Philippe Mori 2-Aug-14 23:14pm    
There is a compiler option to use standard behavior in Visual C++ compiler.

Originally i was declared outside of for loop scope but the standard was modified along the way. Some compiler have an option for standard compliance or backward compatibility.
kkklko 3-Aug-14 2:47am    
I got it,but could you tell me how set the option in visual c++ 6.0?
Philippe Mori 3-Aug-14 7:54am    
See Why doesn't Visual C++ correctly implement for() loop scooping? for more information.

Visual C++ 6.0 is really out of date as the standard has major updates a few time since then and it wasn't even very conformant in its time.

Particulary in those days, the priority was to give good tools for Windows development much more than to be up-to-date with the standard.
kkklko 2-Aug-14 23:19pm    
I got it,but could you tell me how set the option in visual c++ 6.0?
Sergey Alexandrovich Kryukov 3-Aug-14 1:26am    
Why?! it's much better to write reasonable code. Now do you mean to write non-empty statements under the loops?
And I don't see redefinition here. In what line? Are you sure you are showing the same code which you tried to compile?
—SA

it is an old and known problem of the MS compilers, that in for loops declared variables have scope in the remaining part of the function.

In "pure C" it is wrong - but it is or it was "MS Standard".

AFAIK you havent the option to it in the good old VC 6.0. Newer version of VS are really better. I personally liked the 2008 most. You can download the Express Version for free.

Declare the int above all for loops or use different names.

A workaround would be
void main()
{
   for(int i=0;i<10;i++);
    //open a deeper scope	
   {
      for(int i=0;i<10;i++);
   }
}
 
Share this answer
 
v2
Comments
kkklko 3-Aug-14 5:27am    
Thanks,I don't like VS because it is too larger than VC making my pc slow.
Philippe Mori 3-Aug-14 8:24am    
Express edition are a bit faster than regular edition and should run fine on any computer that can run Windows XP or later well (at least 512M of RAM and clock speed in the GHz range).
Philippe Mori 3-Aug-14 8:19am    
As far as I know, standard was updated in 1998. C++ was based on C and at that time, C didn't even allows to declare a variable in for loop expression but C++ did and C was later updated (C99). In C++, they did allows but initialy it was put in the outer scope. Probably an oversight...

By the was, it was probably at that same time (1998) that declaration was allows in other statements like while or if statement.
KarstenK 3-Aug-14 13:09pm    
At Philippe Mori:
1. Express Editios are "lite" Editions. There is no MFC in it. Some elder guys may miss it.
2. in C you MUST declare variables at the beginning, in C++ you havent. That is one of the extensions of "C++"

;-)
Sergey Alexandrovich Kryukov 3-Aug-14 14:11pm    
5ed.
—SA
Quote:
I don't think the 'i' is redefine
Indeed, you are right.

Your compiler is either old or not compliant (or both :-) ). I suggest you getting a more up-to-date compiler.

In C++, variables declared in the for statement are properly scoped in the loop itself, hence no redefinition , no double initialization in your code.

Moreover, standard C++ doesn't allow void man(): the main function must return an int.
 
Share this answer
 
Comments
kkklko 3-Aug-14 5:24am    
Thanks,and it may be the reason that my compiler is too old.
CPallini 3-Aug-14 6:06am    
You are welcome. Please note there are many free compilers available.
Philippe Mori 3-Aug-14 8:28am    
Probably worst are those that are still using Turbo C++...
Sergey Alexandrovich Kryukov 3-Aug-14 14:11pm    
Agree, a 5.
The loop statement is supposed to create a separate context. If the variable is created in inner context (which is the only reasonably practical and recommended way for a loop variable), it is not visible outside. If "redefine" compilation error is shown, it could be considered as a compiler defect.
—SA
CPallini 3-Aug-14 15:53pm    
Thank you, Sergey.
Depends on the compiler: some are more flexible than others.
For some compilers this is a redefinition because they see the i as having a scope limited to the whole main function, rather than just the for loop with which it is associated.

However, the most likely reason you are getting it would be to forget the semicolon at the end of the first loop, which would make it:
C++
#include <iostream>
void main()
{
    for(int i=0;i<10;i++)
        for(int i=0;i<10;i++);
}
Unless you copy'n'pasted your code direct from your source file, I'd have a look for that...

But even if you did, it is a very, very bad practice to use a semicolon at the end of a loop, and if your compiler is not giving you a warning then you need to enable it. If it is giving you a warning, then you need to change it's options to "Treat warnings as errors" - particularly when you are learning.
It's far too easy to write code like this:
C++
#include <iostream>
void main()
   {
   int i;
   for(i=0;i<10;i++);
      {
      printf(i);
      } 
   }
And spend a long time trying to work out what is wrong with your program.
There are other little traps like that you need to make sure teh compielr won;t let you make:
C++
if (a == b);
   c = 0;
And the classic:
C++
if (a = b)
   c = 0;
Making your compiler as strict as possible prevents a lot of hair pulling!
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 3-Aug-14 14:11pm    
5ed.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900