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

'for each' is not a member of std. C++11 support already enabled

本文关键字:启用 支持 C++11 std for each 不是 成员      更新时间:2023-10-16

教授的家庭作业代码如下:这是直接开箱即用的,我没有修改我教授的代码。程序还有更多内容,但这就是错误发生的地方。黑体显示的问题行

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包含行。

关于如何解决这个问题的想法,有人吗?

从评论中确认后:

你忘了包括#include<algorithm>。包括所需的头文件,使符号foreach包含在std名称空间中,从而在程序的其余部分可见。

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

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

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