Like Tree3Likes

range based for command

This is a discussion on range based for command within the C++ Programming forums, part of the General Programming Boards category; I'm using code::blocks w/ minGW. I get an error message at the first for statement for(int& n : arr) Is ...

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    46

    range based for command

    I'm using code::blocks w/ minGW.
    I get an error message at the first for statement

    for(int& n : arr)

    Is this an issue with the compiler not being c++0x compliant?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    No, it's a problem with you not making a proper for loop.
    Code:
    for( initialize_stuff ; truth_test_to_continue_or_not ; increment_stuff )
    {
        do_stuff();
    }
    Everything in this color is optional, everything in bold is not.


    Quzah.
    Last edited by quzah; 08-03-2011 at 09:11 PM. Reason: semi-colons don't take bolding well
    Hope is the first step on the road to disappointment.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,648
    Quote Originally Posted by tabl3six
    I get an error message at the first for statement

    for(int& n : arr)

    Is this an issue with the compiler not being c++0x compliant?
    Yes.

    Quote Originally Posted by quzah
    No, it's a problem with you not making a proper for loop.
    Hence the mention of C++0x, or rather C++11. Refer to clause 6.5.4.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I don't have the C++ standard. Did they suddenly decide you don't need to form proper loops? I knew there was a reason I didn't like C++. Looks like they just gave me another one.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,648
    Quote Originally Posted by quzah
    I don't have the C++ standard.
    I just referred to one of the latest draft versions; the final version has not been published yet (I think), and I don't have my copy of the final draft with me.

    Quote Originally Posted by quzah
    Did they suddenly decide you don't need to form proper loops?
    It certainly is not "suddenly", and it is a proper loop since the language defines what a proper loop is. Basically, it is syntactic sugar for a typical for loop that iterates over some array or container via iterators.
    quzah likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    46
    Refer to clause 6.5.4.

    Where do I find this?

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,648
    Quote Originally Posted by tabl3six
    Where do I find this?
    In the (yet to be published) 2011 edition of the C++ standard. You can find draft versions at the C++ standards committee website.
    quzah likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    MinGW does indeed support C++11. You need to explicitly enable support for it, however, via a command line. There should also be an option for the compiler in the C::B gui.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,499
    Bear in mind that the version of MinGW available in C::B may be a few steps behind the original MinGW port, which in turn may be a few steps behind the current GCC release.

    At a command prompt, type
    $ gcc --version
    and check to see what is supposed to be supported at that version (check the main GCC site).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  10. #10
    Registered User
    Join Date
    Jun 2011
    Posts
    46
    Quote Originally Posted by Salem View Post
    Bear in mind that the version of MinGW available in C::B may be a few steps behind the original MinGW port, which in turn may be a few steps behind the current GCC release.

    At a command prompt, type
    $ gcc --version
    and check to see what is supposed to be supported at that version (check the main GCC site).
    I'm sorry I'm very new to this. Do I bring up the command prompt in windows or is there a way to do it in c::b. I tried the gui setting -std=C++0x, that did not work.

    Thanks everyone for the help.

  11. #11
    Registered User
    Join Date
    Oct 2006
    Posts
    2,370
    as salem said, you need to check the version of g++ that you have. 4.6 is the earliest version that supports range-based for, and on a side note, the nullptr keyword.

  12. #12
    Registered User
    Join Date
    Jun 2011
    Posts
    46
    I've searched for the 4.6 compiler. Doesn't seem to exist. I went to the GCC website and saw a link for it there, but the information is so overwhelming, not sure what to do.

    I would think the author of the book wouldn't put examples of code you couldn't easily run. This seems like pulling teeth to me.

    Think I'm going to ignor the C++0x chapter.

  13. #13
    Registered User
    Join Date
    Jun 2011
    Posts
    2,292
    I don't know if this will work, but have you tried:

    Settings -> compiler and debugger -> global compiler settings

    In the "compiler flags" tab, there is an option that says, "Have g++ follow the coming C++0x ISO c++ language standard"

    Perhaps enabling this option will help?

  14. #14
    Registered User
    Join Date
    May 2010
    Posts
    2,711
    I doubt that will help unless the OP upgrades the compiler to version 4.6 or higher. The newest compiler I found on the MinGW site was version 4.52.

    Jim

  15. #15
    Registered User
    Join Date
    Jun 2011
    Posts
    46
    Quote Originally Posted by Matticus View Post
    I don't know if this will work, but have you tried:

    Settings -> compiler and debugger -> global compiler settings

    In the "compiler flags" tab, there is an option that says, "Have g++ follow the coming C++0x ISO c++ language standard"

    Perhaps enabling this option will help?
    yes, did that. I'm just not sure how to upgrade to 4.6. Maybe I shouldn't.

    I have GCC 4.5.2

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reversing the traversal of c++0x's range based for
    By Mozza314 in forum C++ Programming
    Replies: 29
    Last Post: 03-15-2011, 06:11 PM
  2. Replies: 5
    Last Post: 11-27-2010, 11:42 AM
  3. Conditional if based on command line arguments problem
    By DimensionX in forum C Programming
    Replies: 4
    Last Post: 11-05-2009, 03:38 AM
  4. Get pid based on command
    By 3saul in forum Linux Programming
    Replies: 1
    Last Post: 07-01-2006, 12:52 AM
  5. Range
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 05-23-2002, 10:52 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21