Click here to Skip to main content
15,867,298 members
Please Sign up or sign in to vote.
2.00/5 (3 votes)
See more:
1>d:\services\winservice\service.cpp(408) : error C2065: 'i' : undeclared identifier
1>d:\services\winservice\service.cpp(408) : error C2065: 'i' : undeclared identifier
1>d:\services\winservice\service.cpp(408) : error C2065: 'i' : undeclared identifier
1>d:\services\winservice\service.cpp(410) : error C2065: 'i' : undeclared identifier

C#
else if(nIndex==127)
{
    for(int i = MAX_NUM_OF_PROCESS-1; i >= 0; i--)
    {
        EndProcess(i);
    }
    for(i = 0; i < MAX_NUM_OF_PROCESS; i++)    // Line 408
    {
        StartProcess(i);                          // Line 410
    }


Please can anyone HELP
I am a Alaska Xbase++ Programmer
email - xxx@xxx.com [Email removed]
Thanks
Posted
Updated 28-Aug-12 3:03am
v2
Comments
Legor 28-Aug-12 9:04am    
I removed your Email adress since it will only reward you with lots of Spam if you give your email away like this in public message boards.

And by the way the error message you posted petty clearly states whats going wrong. You variable i is not defined in the second for loop. See the solutions below.
pasztorpisti 28-Aug-12 11:28am    
With older c++ compilers (like VC++6) the variable doesn't go out of scope at the end of the for loop body.
Sumal.V 29-Aug-12 7:36am    
Hey there are 5 solutions posted for you. Please accept a solution, so that this question goes off the unanswered list.

Or if you are still having problems, ask!

This was compatible by default for some earlier versions of C++, but is no more good by default. I guess this can be changed by some option, but better is to change the code itself.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 9-Aug-13 2:26am    
Actually, this is the best answer so far. No wonder it took no votes. :-)
—SA
Hi, taking the following code for example:
C++
for (int i= 0; i < 10; i++)
{
    int nValue= i;
}
for (i = 0; i < 6; i++)
{
    int nValue2= i;
}

This code works well for visual C++ 6.0. But in the later version of visual C++, the compiler has emhanced for loop scope. So, this code can't compile by visual studio 2005. There are two ways to solve this problem. First, which I recommand, you may follow the Solution1,Solution2 or Solution3 method to change the code to :
C++
for (int i= 0; i < 10; i++)
{
    int nValue= i;
}
for (int i = 0; i < 6; i++)
{
    int nValue2= i;
}


The second way is changing the visual studio project setting. Please find this item: "property pages"->"Configuration propertites"->"C/C++"->"Language"->"Force Conformance In For Loop Scope" then change its default value "Yes" to "No". After that try to build the original code. Good luck to you.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 9-Aug-13 2:26am    
5ed.
—SA
The variable i that you declared in the first loop goes out of scope at the end of the first loop. That means that it is no longer declared and you can’t continue to use that same variable. You can re-declare i in the second loop, which is what I usually do. Since the two loops are unrelated and to emphasize that you are, in fact, using a different variable I have declared a second variable j in the second loop in the code below.

C#
for(int i = MAX_NUM_OF_PROCESS-1; i >= 0; i--)
    {
        EndProcess(i);
    }
    for(int j = 0; j < MAX_NUM_OF_PROCESS; j++)
    {
        StartProcess(j);
    }
 
Share this answer
 
v2
Comments
Philip Stuyck 28-Aug-12 9:03am    
I would continue to use i, since i is scoped for within the loop only. If you use j, or change the loop variable name, you are actually missing the point of what scoping really means. It is not even required according to coding rules.
On top of this it is quite clear from the code that the two loops are unrelated so actually using the first option with
for (int i=... is the best solution instead of the second solution that I provided for completenes
BillW33 28-Aug-12 10:31am    
I did not explain my reasoning very clearly. I have improved my explanation.
C#
for(int i = MAX_NUM_OF_PROCESS-1; i >= 0; i--)
    {
        EndProcess(i);
    }
    for(int i = 0; i < MAX_NUM_OF_PROCESS; i++)
    {
        StartProcess(j);
    }

if you don't want this do this :

C#
int i;
for(i = MAX_NUM_OF_PROCESS-1; i >= 0; i--)
    {
        EndProcess(i);
    }
    for(i = 0; j < MAX_NUM_OF_PROCESS; j++)
    {
        StartProcess(j);
    }
 
Share this answer
 
v4
That is because the " i " you declared in the for loop is local to that loop.
That is, it is valid as long as the for loop exists. But if you want to use that variable in multiple for loops, you can do this :

Declare i outside the loop ; And use this in any of your loops
C++
int i; //declare i outside the loop.

for(i = MAX_NUM_OF_PROCESS-1; i >= 0; i--)
{
    EndProcess(i);
}
for(i = 0; i < MAX_NUM_OF_PROCESS; i++)    // Line 408
{
    StartProcess(i);                          // Line 410
}


This will not give you any error.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 9-Aug-13 2:24am    
This is really, really, really bad practice to declare a loop variable outside the loop.
Just a destructive advice. Not programming at all is much better than shutting up compilation errors.
—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