JohnG
JohnG

Reputation: 2109

foreach not recognized in C++

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?

Upvotes: 3

Views: 13354

Answers (5)

templatetypedef
templatetypedef

Reputation: 373082

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.

Upvotes: 9

helloyou
helloyou

Reputation: 11

Qt is support foreach, using like this:

QDir dir("Dir");
dir=QFileDialog::getExistingDirectory(0,"Select Folder: ");
QFileInfoList list = dir.entryInfoList(QDir::Dirs| QDir::Files | QDir::NoDotAndDotDot);

std::vector<std::string> names;
foreach(QFileInfo finfo, list){
    std::string str=dir.path().toStdString().c_str();
    str=str+"/";
    names.push_back(str+finfo.fileName().toStdString().c_str());
}

but, when you using #define QT_NO_KEYWORDS on header file, foreach disabled.

Upvotes: -1

wkl
wkl

Reputation: 80031

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?

Upvotes: 3

Scott Langham
Scott Langham

Reputation: 60411

foreach doesn't exist in C++.

In the latest version of C++ which is only just released in some of the latest compilers, you can use "Range-based for-loop" .. find it on this page: http://en.wikipedia.org/wiki/C%2B%2B0x

I doubt though that your compiler supports that. So, maybe stick with a for loop for now.

Upvotes: 1

Gene Bushuyev
Gene Bushuyev

Reputation: 5538

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

Upvotes: 2

Related Questions