Sign in to follow this  
Mantear

Visual Studio 2005 Express

Recommended Posts

Mantear    251
I downloaded VS 2005 Express today. I figured it was about time to give version 6 the boot. I'm getting used to the way things work in it, and ran across an odd thing. I've been using CodeWright to edit my files, which lets me easily comment out sections of code using '//' instead of '/* ... */'. However, the VS 2005 compiler seems to not like that. It seems to think the block of comments is the start of a new function, making my local variables undeclared. Anyone know a way around this? Thanks.

Share this post


Link to post
Share on other sites
Mantear    251
Well, it appears that it doesn't like
for (int i = 0; i < some_num; i++)
{ ... }

for (i = 0; i < some_num2; i++)
{ ... }
It wants me to re-declare 'i' for the second for loop. Is this because it is more strictly compliant, or is this something MS dreamed up? I haven't seen another C++ complain about it.

Share this post


Link to post
Share on other sites
Boku San    428
That's an MS quirk -- you're using '05, right? I'd thought they'd have this cleared up by now.

You'll need to redeclare i in that for loop again -- I saw a workaround for it on flipcode, but, as we all know...that place is dead now. You can still search for it, if you like, it detailed a total replacement for for as a macro, which worked properly.


EDIT: Found the link to the article -- like SiCrane mentions below, I had thought this problem had been fixed. Workaround is here if you need it -- but, again, are you SURE you're using MSVC8 ('05)?

Share this post


Link to post
Share on other sites
SiCrane    11839
According to standard C++, variables declared in for loops go out of scope when the for loop ends. MSVC 6 was broken with respect to this. MSVC 8 does it correctly.

Share this post


Link to post
Share on other sites
Mantear    251
Yes, I'm using '05. Just downloaded and installed it.

I'm somewhat surprised that the 'old way' is wrong. I've used a few other compilers (Green Hills, Code Composer, Visual DSP), and they all accepted the single declaration. How do most people handle their for-loop variables? Declare them at the top, ala C style? Or re-declare them each time?

Share this post


Link to post
Share on other sites
Boku San    428
Quote:
Original post by Mantear
Yes, I'm using '05. Just downloaded and installed it.

I'm somewhat surprised that the 'old way' is wrong. I've used a few other compilers (Green Hills, Code Composer, Visual DSP), and they all accepted the single declaration. How do most people handle their for-loop variables? Declare them at the top, ala C style? Or re-declare them each time?


Quote:
Original post by SiCrane
According to standard C++, variables declared in for loops go out of scope when the for loop ends. MSVC 6 was broken with respect to this. MSVC 8 does it correctly.


Emphasis in bold -- ...How sure are you that you got the right version again? ([lol])

Share this post


Link to post
Share on other sites
Mantear    251
Quote:
Original post by Boku San
Emphasis in bold -- ...How sure are you that you got the right version again? ([lol])


Yes... I am sure. The old way let you declare a variable in a for-loop statement, and you no longer had to declare it again even for a for-loop that came after the previous one. That's how MSVC 6 did it, that's how the other compilers I've used have done it. That's against the standard that SiCrane stated. MSVC 8 does it correctly, in other words, I need to re-declare the variable if I want to use it in the next for-loop. I'm not sure what your point was.

Share this post


Link to post
Share on other sites
Slaru    222
Quote:
Original post by Mantear
How do most people handle their for-loop variables? Declare them at the top, ala C style? Or re-declare them each time?


I declare them for each for-loop almost always. I figure compilers should be smart enough (at least smarter than me) to be able to optimize things best. This is just me though, and I'm definitely not a consumate (but I am proficient) C++ programmer.

Share this post


Link to post
Share on other sites
superpig    1825
The quick solution:

Go to Project->Properties
Configuration Properties
C/C++
Language

Set "Force Conformance In For Loop Scope" to "No"

However, it'd be better to fix up your for loops.

Share this post


Link to post
Share on other sites
Boku San    428
Quote:
Original post by Mantear
Quote:
Original post by Boku San
Emphasis in bold -- ...How sure are you that you got the right version again? ([lol])


Yes... I am sure. The old way let you declare a variable in a for-loop statement, and you no longer had to declare it again even for a for-loop that came after the previous one. That's how MSVC 6 did it, that's how the other compilers I've used have done it. That's against the standard that SiCrane stated. MSVC 8 does it correctly, in other words, I need to re-declare the variable if I want to use it in the next for-loop. I'm not sure what your point was.


My apologies there -- I actually misread your quote that I'd posted there. As posters above said, it's better to redeclare your temp variables per-for-loop.

Share this post


Link to post
Share on other sites
Mantear    251
It's ok Boku, mistakes happen. And to make up for it, you can help me with another problem related to MSVC8! My project is now compiling, but I'm hitting a whole host of linking errors. Some are OpenGL related, which doesn't surprise me, but others are not. For example, it cannot link DispatchMessage, PeekMessage, PostQuitMessage, MessageBox, etc. I didn't start a new project, rather, just opened the old MSVC6 one. Any ideas as to what I'm missing?

Share this post


Link to post
Share on other sites
Boku San    428
Quote:
Original post by Mantear
It's ok Boku, mistakes happen. And to make up for it, you can help me with another problem related to MSVC8! My project is now compiling, but I'm hitting a whole host of linking errors. Some are OpenGL related, which doesn't surprise me, but others are not. For example, it cannot link DispatchMessage, PeekMessage, PostQuitMessage, MessageBox, etc. I didn't start a new project, rather, just opened the old MSVC6 one. Any ideas as to what I'm missing?


This is going to be painful. I promise. But instead of importing the old project, create a new Win32 Application and manually add the files.

I had quite a few linker errors when I tried this myself.

Alternatively, you could probably try explicitely linking user32.lib, kernel32.lib, etc./whatever, but they'll be linked properly in a new project.

Share this post


Link to post
Share on other sites
Mantear    251
Yes, I installed the platform SDK. I'm still unsure of how to go from my old project to the new one. In VC6, I created a Win32 Application (not a console one). I can't quite find the equivalent in '05, with the stdafx files, etc.

Share this post


Link to post
Share on other sites
Boku San    428
Quote:
Original post by Mantear
Yes, I installed the platform SDK. I'm still unsure of how to go from my old project to the new one. In VC6, I created a Win32 Application (not a console one). I can't quite find the equivalent in '05, with the stdafx files, etc.


If you make a New Project File, you need to set it as a Console Project and check "Empty Project" if you want to exclude the stdafx junk.

Hope that helps.

Share this post


Link to post
Share on other sites
Ravyne    14300
Also, one thing I ran into when porting my library from VS2003 (7.1) to VS2005 (8) is that VC 8 wants to link against a new version of the C std library versus the old version. If you're porting old code that expects to be linked against the old C lib, it'll cause you some problems.

Also, The compiler will throw warnings that the old-style functions are depreciated, although this can be ignored.

Doesn't seem like you've hit that snag yet, but its something to be aware of porting from previous VC versions, particularly since you're coming from as far back as 6 (Its ok though, we all used 6 back in the day, you're forgiven ;) )

Share this post


Link to post
Share on other sites
Dave Hunt    4872
Quote:
Original post by Mantear
Yes, I installed the platform SDK. I'm still unsure of how to go from my old project to the new one. In VC6, I created a Win32 Application (not a console one). I can't quite find the equivalent in '05, with the stdafx files, etc.


To enable the Win32 application template, you need to follow the instructions here. It's not enabled out of the box for some reason (probably because you have to get the Platform SDK for it to be useful).

Share this post


Link to post
Share on other sites
Mantear    251
Does anyone know if there will be a future realse that removes the need for all these work-arounds? Or is it just because I'm trying to port from an old version, and they've just given up on MSVC6?

Share this post


Link to post
Share on other sites
Ravyne    14300
Its not so much that *They* have given up on VS6, but that *everyone* has given up on VS6. It was pretty good at the time, but It came out just before/right around the time that a new C++ standard was ratified. As such, its behavior doesn't conform correctly. The scoping issues in loops are a prime example. Also, the STL implimentation was poor.

The bottom line is that you shouldn't worry about keeping compatibility with VS6 and neither should Microsoft; It would be INCORRECT to do so. It just goes to show that alot of broken code was written, broken in the respect that the code itself was not written with regard to the standard. Well written code should not break with the transition, because well written code was written in a standards-conforming matter, even if the compiler would have allowed otherwise. Thats not an attack or anything, I'm sure the vast majority of code and coders have errors related to this issue.

Rather than looking at the new way as a work-around from the old way, its more accurate to say that the old way was a work-around of the standard.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this