foreach在C ++中不被识别 [英] foreach not recognized in C++
问题描述
在我的cs106b书中,我们使用表达式foreach来查看Map中的单词列表。我执行了代码,把我的头撞在了面对神秘错误的墙上。代码如下:
$ b $ $ pre $ void $ DisplayWordCounts(Map< int>& wordsCount){
foreach(字串中的字数) ){
cout<<左<< (15)<字<<正确<< (5)
<< wordsCount [word]<< ENDL;
$ / code>
以foreach开头以下错误:
lesson4-macbeth / life.cpp:58:错误:期望在word之前的主要表达式
lesson4-macbeth / life.cpp:58:错误:'foreach'未被声明这个范围
lesson4-macbeth / life.cpp:58:error:expected`;'before'{'token
我猜foreach是不被识别的。在这种情况下,我怎样才能通过Map类的项目列表?
#includeforeach.h
,你应该可以使用它。如果你好奇,我可以尝试发送你的文件的源代码。另外,找到堆栈溢出的道具!虽然你的SL应该可能是你的第一道防线。 : - )
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?
foreach
is not a standard C++ feature. I actually helped Eric Roberts put together the implementation for CS106B/X, and you should be able to use it if you #include "foreach.h"
. If you're curious, I can try to send you the file's source code.
Also, props for finding Stack Overflow! Though your SL should probably be your first line of defense. :-)
这篇关于foreach在C ++中不被识别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!