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

时间:2015-06-08 10:23:33

标签: c++ c++11 netbeans std codeblocks

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

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

如何解决这个问题的想法,任何人?它过去常常给我所有位置的错误。

2 个答案:

答案 0 :(得分:11)

从评论中确认后:

您忘了加入#include<algorithm>。包含所需的标头,使foreach名称空间中包含符号std,从而对程序的其余部分可见。

答案 1 :(得分:3)

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

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

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