Need immediate help on basic cpp
Hi guys,
I know this forum is dedicated to qt but I am in trouble with basic cpp concept.
Actually the following code works fine on window with vs2003 but won't get compiled on linux and mac os.
Code:
void functionname()
{
// few statements
for(int nIndex = 0; nIndex < 10; nIndex++)
{
// few statements
}
for(nIndex = 0; nIndex < 10; nIndex++)
{
// few statements
}
// few statements
}
I think it is incorrect because nIndex variables should not exist after for loop. Please let me know your opinions.
Any body can help me or figure out the reason behind the problem.
Thanks in Advance.
Sandip
Re: Need immediate help on basic cpp
Compilers behave different on this topic. (Well actually MSVC is wrong ;-) )
MS fixed this with VS 2005 - amoung a lot of other stuff!
Re: Need immediate help on basic cpp
Quote:
Originally Posted by
DeepDiver
Compilers behave different on this topic. (Well actually MSVC is wrong ;-) )
MS fixed this with VS 2005 - amoung a lot of other stuff!
Thanks!
I will try it on VS 2005.
Re: Need immediate help on basic cpp
It's enough to add "int" before "nIndex" in the second loop and it should compile fine on every compiler understanding C++ standard (which means every not-very-ancient compiler excluding VC6).
Re: Need immediate help on basic cpp
Agree with wysota,,,
in standard compilers, the variable inside the loop has scope of the loop itself.
Hence in second loop, u need to specify int before the variable
Re: Need immediate help on basic cpp
Yes both of you are right.
But I was wondering because VS2003 allows it without warning.
It is surprised me.
Thanks!
Re: Need immediate help on basic cpp
That's because VC2003 tries to be compliant with VC6 which is compliant with draft of C++ standard which is not compliant with C++ standard.
Re: Need immediate help on basic cpp
Hi,
Thanks a lot!
This is my last question on this topic.
Can you give me name of one compiler(from Microsoft) which will give me an error - 'undeclared identifier'?
I have tried it on VS 2005 also.
Thanks once again.
Re: Need immediate help on basic cpp
Probably no MS compiler will complain about it as it tries to be compatible with VC6 (by breaking a standard, although you might call it an "extension to the standard" if you want, still the code is not valid C++).
Re: Need immediate help on basic cpp
Regardless the compiler we use, shouldn't it be a good c++ practice to declare
the variable nIndex in the beginning of the function before the two for's ???
Re: Need immediate help on basic cpp
Imo, no.
I think it is good practice to declare a variable as local as possible.
Otherwise:
* it is not clear (w/o inspectiong more code) if the variable is relevant for more core
* it might get used twice (as in this example): is the value of the variable after the (first) loop important?
By declaring it inside the for (or the if), it is made obvious that this "helper" variable is not used somewhere else. This makes the code more easily understandable (to me, at least).