CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums
Results 1 to 3 of 3

Thread: ForEach error

  1. #1
    Join Date
    Nov 2006

    Question ForEach error

    Hello everyone, I finally bought a C++ For Dummies book and can't be any happier learning from it, I've followed the books directions as it instructed and fell into a bit of trouble. Normally I trust the author corrects their own misspellings/mistakes and (usually) over look little problems up until this one.

    /*   ForEachDemo -  C++ includes a form of the "for each"
                        which iterates through each member of
                        a list
    #include <limits> 
    #include <cstdlib>
    #include <iostream>
    using namespace std;
    int main(int nNumberofArgs, char* pszArgs[])
        cout << "The primes less than 20 are:" << endl;
        for(int n : {1, 2, 3, 5, 7, 11, 13, 17, 19} )
                cout << n << ", ";
        cout << endl;
        // wait until user is ready before terminating program
        // to allow the user to see the program results
        cout << "Press Enter to continue..." << endl;
        cin.ignore(std::numeric_limits<streamsize>::max(),'\n'); // This snippet was cut/pasted from another forum 
                                                                                                             // The original cin.ignore(10,'\n\'); <-- looked like this
        return 0;
    This function is meant to loop the list inside the for loop until it is complete then terminates. The error I get follows,,,

    error: deducing from brace-enclosed initializer list requires '#include <initializer_list>'
    So I added #include <initializer_list> at the top and got another error. When I compiled this code in Linux using g++ i had no trouble. The book asks me to use Code::Blocks and I'm using the latest compiler, so I checked the settings and I prefered c++17 over c++11 (which makes sense, no?). This is a Developers refresh for me so I'm starting all over hoping to be a experienced coder.

    P.S. Like I said, i compiled this in Linux so I'm looking @ Code::Blocks as to what I'm _not_ doing correctly, help!


  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012

    Re: ForEach error

    For Windows MS VS2022, this compiles OK:

    #include <limits>
    #include <iostream>
    int main() {
    	std::cout << "The primes less than 20 are:\n";
    	for (int n : {2, 3, 5, 7, 11, 13, 17, 19})
    		std::cout << n << ", ";
    	std::cout << '\n';
    	std::cout << "Press Enter to continue...";
    	std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    I don't use LInux or Code::Blocks so can't comment/help with those. Sorry.

    NB. 1 is not a prime!

    Also note that Code::Blocks is just an IDE. By itself it's not a compiler. One has to be used with Code::Blocks. Which one/version are you using with it?

    Code::Blocks supports gcc so as you already have gcc, you should be able to integrate that with Code::Blocks.

    Multiple compiler support:
    GCC (MingW / GNU GCC)
    Digital Mars
    Borland C++ 5.5
    Open Watcom
    …and more
    I prefered c++17 over c++11
    The current version of C++ is C++20. For C++20, gcc needs at least v11. The latest version is v13. The version of C++ changes every 3 years. The next version is C++23 which is now feature-complete and will be officially released later in the year. See:
    Last edited by 2kaud; September 3rd, 2022 at 05:38 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.3.5)

  3. #3
    Join Date
    Nov 2006

    Re: ForEach error

    Which one/version are you using with it?
    Right now I'm using Code::Blocks 20.03 the latest version to revamp my coding skills. The very IDE the book suggested to use for most operating systems. The error I got suggested me to enable the -std=c++17 flag in the compiler settings. So once I did that the error went away and I was able to compile it. It's a lesson to take the time to read the warnings/errors to understand and debug your own code.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Click Here to Expand Forum to Full Width