Results 1 to 11 of 11

Thread: Need immediate help on basic cpp

  1. #1
    Join Date
    Feb 2008
    Posts
    51
    Thanks
    3
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Question 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.

    Qt Code:
    1. void functionname()
    2. {
    3. // few statements
    4.  
    5. for(int nIndex = 0; nIndex < 10; nIndex++)
    6. {
    7. // few statements
    8. }
    9.  
    10.  
    11. for(nIndex = 0; nIndex < 10; nIndex++)
    12. {
    13. // few statements
    14. }
    15.  
    16. // few statements
    17. }
    To copy to clipboard, switch view to plain text mode 

    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
    Last edited by jpn; 25th August 2008 at 08:58. Reason: missing [code] tags

  2. #2
    Join Date
    Oct 2007
    Location
    Munich, Bavaria
    Posts
    144
    Thanks
    1
    Thanked 19 Times in 19 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default 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!

  3. #3
    Join Date
    Feb 2008
    Posts
    51
    Thanks
    3
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Need immediate help on basic cpp

    Quote Originally Posted by DeepDiver View Post
    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.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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).

  5. #5
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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

  6. #6
    Join Date
    Feb 2008
    Posts
    51
    Thanks
    3
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default 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!

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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.

  8. #8
    Join Date
    Feb 2008
    Posts
    51
    Thanks
    3
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default 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.
    Last edited by Sandip; 25th August 2008 at 10:46. Reason: To add additional information

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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++).

  10. The following user says thank you to wysota for this useful post:

    Sandip (26th August 2008)

  11. #10
    Join Date
    Aug 2008
    Location
    Algarve, Portugal
    Posts
    288
    Thanks
    23
    Thanked 32 Times in 28 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default 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 ???

  12. #11
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default 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).

Similar Threads

  1. very simple and basic FAM/Gamin wrapper for qt
    By momesana in forum Qt-based Software
    Replies: 0
    Last Post: 8th April 2008, 12:46
  2. Basic Qt4 tutorial needed
    By pthomas in forum Qt Programming
    Replies: 26
    Last Post: 16th October 2006, 15:11
  3. Need Basic html Browser
    By awalesminfo in forum Newbie
    Replies: 6
    Last Post: 21st March 2006, 17:14
  4. Basic question on new and delete
    By jcr in forum General Programming
    Replies: 25
    Last Post: 14th February 2006, 15:09
  5. Qt/C++ basic Q-s
    By jamadagni in forum General Programming
    Replies: 9
    Last Post: 10th February 2006, 08:01

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.