“for each”不是std的成员。已启用C++11支持。

12

我教授作业的代码如下。这是直接拿来就用的,我没有修改过我的教授的代码。程序还有更多内容,但这就是错误发生的地方。问题行已加粗。

std::cout << "\nAll remaining courses with enrollments:\n";
allCourses = WSUCourse::getAllCourses();
std::for_each(
  allCourses.begin(),
  allCourses.end(),
  WSUCoursePrinter());

我收到了以下错误信息。

g++    -c -g -std=c++11 -MMD -MP -MF "build/Debug/Cygwin_4.x-Windows/main.o.d" -o build/Debug/Cygwin_4.x-Windows/main.o main.cpp
main.cpp: In member function 'void WSUStudentPrinter::operator()(const WSUStudent*)':
main.cpp:26:20: error: 'to_string' is not a member of 'std'
       std::cout << std::to_string(studentPtr->getUniqueID()) <<
                    ^
main.cpp: In function 'void test()':
main.cpp:162:4: error: 'for_each' is not a member of 'std'
    std::for_each(
    ^
main.cpp:174:4: error: 'for_each' is not a member of 'std'
    std::for_each(
    ^
nbproject/Makefile-Debug.mk:84: recipe for target 'build/Debug/Cygwin_4.x-Windows/main.o' failed
总之,这就像标题所说的一样。 "'For each' is not a member of std"
我已经在编译器中启用了所有IDE的标准11支持。在代码块中,我将参数放在编译器设置中,在Netbeans中,我在编译器的项目属性下进行了更改,即使是校园网络上的Linux系统也无法运行它,出现相同的错误,并且我使用了标准11包含行。
有人对如何修复此问题有什么想法吗?以前它在所有位置都给我相同的错误。

10
你是否包含了“算法”? - Mohit Jain
2
非常感谢你。你不知道我已经在做这个任务上工作了多长时间了。老师的代码一开始就有问题。我该如何将你标记为已回答的评论? - user3424549
1
我将评论改写为答案,如果答案解决了您的问题,您可以点击答案旁边的勾号。如果您的问题仍未得到解答,您可以编辑您的问题以获得更多/更好的答案。 - Mohit Jain
2个回答

24

从评论中确认:

您忘记包含 #include<algorithm>。 包含必需的头文件以使符号 foreach 包含在 std 命名空间中,因此对程序的其余部分可见。


4

您的解决方案纯粹是C++98,一个可能的C++11解决方案看起来像这样:

std::cout << "\nAll remaining courses with enrollments:\n";
for (auto & course : WSUCourse::getAllCourses())
{
    //whatever WSUCoursePrinter does.
    course.print() ;
}

我更喜欢这种方式,它不需要算法标题。


2
一个 C++11 的解决方案 - Johann Gerell

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接