Thread: range based for command

  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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.

  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
    3,445
    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
    4,513
    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
    4,632
    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

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, 12:42 PM
  3. Conditional if based on command line arguments problem
    By DimensionX in forum C Programming
    Replies: 4
    Last Post: 11-05-2009, 04: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