foreach not recognized in C++

10,519

Solution 1

foreach is not a standard C++ feature. This was something Eric Roberts and I developed for the Stanford introductory programming sequence and predates the more modern C++11 range-based for loop. Now that C++11 compiler support is more widespread, we've stopped using foreach and just opted to go with the standard C++ enhanced for loop.

I would generally not advice using foreach going forward as it's nonstandard. However, if you're compiling older code that uses it, you'll need to include one of the header files from the Stanford C++ Libraries that defines it.

Solution 2

I suggest for_each.

Solution 3

What book are you using?

foreach is not a C++ keyword, and I think the closest extension that introduces it, with that specific syntax, into the language is in Visual C++, as described in this link: http://blogs.msdn.com/b/arich/archive/2004/09/08/227139.aspx

There is for_each in <algorithm>, but its signature is very different from what you're using (which is a very Java for-each syntax).

Also I notice that you're using Map which is different from std::map?

Solution 4

Because the function name is for_each P.S. I thought it was a c++ question, as the tag suggested, but the syntax all wrong for C++.

Solution 5

foreach is not a construct in C++!

Share:
10,519
JohnG
Author by

JohnG

Updated on June 05, 2022

Comments

  • JohnG
    JohnG about 2 years

    In my the cs106b book we use the expression "foreach" to go through a list of words in a Map. I implemented the code and banged my head against the wall facing mysterious errors. Here's the code:

       void DisplayWordCounts(Map<int> & wordsCount) {
         foreach (string word in wordsCount) {
            cout << left << setw(15) << word << right << setw(5)
            << wordsCount[word] << endl;
         }
    }
    

    on the line starting with "foreach" I get the following errors: lesson4-macbeth/life.cpp:58: error: expected primary-expression before 'word' lesson4-macbeth/life.cpp:58: error: 'foreach' was not declared in this scope lesson4-macbeth/life.cpp:58: error: expected `;' before '{' token

    I guess foreach is not recognized. In that case, how can I go through a list of items from the Map class?

  • Oliver Charlesworth
    Oliver Charlesworth over 13 years
    Well it is. But you can't use it like that.
  • templatetypedef
    templatetypedef over 13 years
    @Oli Charlesworth- There wasn't anything wrong per se with anything in C++. The main advantage was simplicity - it's much easier to focus on more advanced ideas like recursion, linked lists, graphs, and asymptotic analysis if you don't have to introduce complex iterator syntax to look at everything in a map/set/etc. The curriculum will probably soon be updated to go over STL containers at the end of the course, meaning that foreach will likely be "training wheels" to simplify the material.
  • Clifford
    Clifford over 13 years
    Why would you have done such an evil thing!? If you want to teach such a construct, why not use a language that supports it directly. It reminds me of the CS graduates I used to come across who, faced with having to use C in the real world, would #define { and } as BEGIN and END to make thier code a bit more Pascal-like!
  • templatetypedef
    templatetypedef over 13 years
    @Clifford- The point of these courses is to get students up to speed on language-agnostic concepts like recursion, data structures, inheritance, etc. rather than to produce hardcore C++ programmers. There are other courses that focus on the specifics of C++ and other programming languages. From experience this setup actually works quite well, since students usually take it upon themselves to learn the nuances of their favorite languages. I'd prefer not to get into an argument about this since I don't think anyone's going to change their mind, but you do have a very good point.
  • Oliver Charlesworth
    Oliver Charlesworth over 13 years
    C++ was an interesting choice for teaching data structures whilst trying to ignore language-specific details!
  • Clifford
    Clifford over 13 years
    @templatetypedef: I realise that, but this student does not apper to have had that made clear to him, and thinks that he is learning C++. I used to be a regular on a forum where similar problems occurred perennially with US students who learned C++ on the AP Computer Science programme; some of them appeared to have made it to the workplace before finding out the courseware class libraries they'd learned were unavailable outside acedemia.
  • Clifford
    Clifford over 13 years
    Despite my opinion on the wisdom of this practice, this answer has to get an up-vote since it exactly answers the question, and clears up John's misunderstanding.
  • Clifford
    Clifford over 13 years
    Using an iterator would be a closer match to the foreach construct, though the syntax is somewhat cumbersome perhaps.
  • Lightness Races in Orbit
    Lightness Races in Orbit over 13 years
    Maps aren't indexed like that. Use an iterator.
  • JohnG
    JohnG over 13 years
    Thanks everyone @templatetypedef thanks for the explanation. @Clifford - cs106b makes it clear that they are using special classes they created - even if foreach.h was not explicitly mentioned, I should have guessed I had to import the class. I am eager to discover C# since it uses foreach.
  • JohnG
    JohnG over 13 years
    @templatetypedef How do I contact you for the source code? I don't see any feature on stackoverflow to get in touch.
  • templatetypedef
    templatetypedef over 13 years
    @JohnG- It's probably best to contact your SL directly and ask for the source. It should be bundled with the standard starter code, though, and so you shouldn't need to explicitly add it in anywhere.
  • JohnG
    JohnG over 13 years
    @templatetypedef I am in DIY mode :) thanks anyway. cs106b rocks! If you want some feedback from your CS online courses, drop me a line [email protected] - I work on University Ave in PA.