Thread: Variable Scope in MSVC++ 6

  1. #1
    Amazingly beautiful user.
    Join Date
    Jul 2005
    Location
    If you knew I'd have to kill you
    Posts
    254

    Variable Scope in MSVC++ 6

    I recently tried to compile one of the programs I'd written with GCC under MSVC++. This is not the program I wrote, but an example of the problem.

    Code:
    for (int i = 0; i < 10; i++)
    {
       std::cout << i << std::endl;
    }
    
    
    for (int i = 10; i > 0; i--)
    {
       std::cout << i << std::endl;
    }
    For some reason, VC++ gives the variable "i" the scope of the code block instead of the for loop. Thus, the second declaration of "i" throws an error, while it shouldn't, as "i" should be local to the for loop.

    So far, the only solution I have found is to put the loops within their own code blocks.


    Code:
    {
      for (int i = 0; i < 10; i++)
      {
         std::cout << i << std::endl;
      }
    }
    
    {
      for (int i = 10; i > 0; i--)
      {
         std::cout << i << std::endl;
      }
    }
    This looks nasty and is something of a pain.
    First off, is this fixed in VS.NET? Second off, is there any way to correct MSVC++ 6's behavoir?
    Last edited by CrazyNorman; 02-13-2006 at 10:23 AM. Reason: Fixed some indentation I didn't like
    Programming Your Mom. http://www.dandongs.com/

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    You're better off just not redeclaring it and just assigning it. Or better off don't declare it in any for loop and declare it in the scope of main like you did in C.
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Thus, the second declaration of "i" throws an error, while it shouldn't
    I've always gotten that error and wondered why myself, but as Sly said you can just declare i on the line before the first for-loop.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    you get that error because VC++ 6.0 is NOT ios standard compilant compiler because it is too old and because M$ likes to do its own thing. The current compiler is supposed to more compliant.

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by Ancient Dragon
    and because M$ likes to do its own thing.
    Hehehe... you know that's just how they roll down in the West-si-yead.
    Sent from my iPadŽ

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > For some reason, VC++ gives the variable "i" the scope of the code block instead of the for loop
    Traditional C++ (which VC6 implements in its own way) makes the scope of i extend to the end of the block
    New standard C++ makes the scope of i explicitly for the loop alone.

    > First off, is this fixed in VS.NET?
    Yes it should be

    > Second off, is there any way to correct MSVC++ 6's behavoir?
    Only by using additional braces, or comment out the second 'int'.

    GCC on the other hand provides
    -ffor-scope
    -fno-for-scope
    to assist with this portability issue.

  7. #7
    chococoder
    Join Date
    Nov 2004
    Posts
    515
    As Salem said, this has nothing to do with Microsoft "liking to do things their way" and everything with the C++ standard having changed to something more sensible since VC6 was created.
    That's one of the penalties of using a decade old product, I expect you'd get pretty much the same when compiling against Borland C++ 4.0 for example.

  8. #8
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Perhaps if you got a more recent compiler, eg: DevC++9.92 or MSVC++ 2003, 2005 this problem would not occur. As Microsoft upgraded the compilers, the older ones no longer understand the newer syntax. DevC++ is free and at the moment you can download MSVC++ 2005 from the website. I had MSVC++6.0 but it gave me to much grief and kept complaining at silly errors that I knew were correct. I tyed these same programs and got this:

    Code:
    Build 1 suceeded, 0 failed, 0 skipped  ERRORS 0 WARNINGS 0
    I just wish all C++ compilers were at the same level of dependence

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. variable scope
    By happyclown in forum C Programming
    Replies: 4
    Last Post: 03-02-2009, 06:48 AM
  2. global and static variable in a class delivered in a DLL
    By George2 in forum C++ Programming
    Replies: 16
    Last Post: 04-13-2008, 08:19 AM
  3. Variable scope
    By Axel in forum C Programming
    Replies: 2
    Last Post: 09-19-2005, 08:41 PM
  4. variable in file scope ???
    By howhy in forum C++ Programming
    Replies: 5
    Last Post: 08-30-2005, 04:46 AM
  5. Use of variable
    By alice in forum C Programming
    Replies: 8
    Last Post: 06-05-2004, 07:32 AM