【问题标题】:'for each' is not a member of std. C++11 support already enabled'for each' 不是 std 的成员。已启用 C++11 支持
【发布时间】:2015-08-22 18:32:13
【问题描述】:

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

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

关于如何解决这个问题的想法,有人吗?它曾经给我所有位置的相同错误。

【问题讨论】:

  • 您是否加入了algorithm
  • 非常感谢。你不知道我在这个任务上工作了多久。老师的密码开箱即用。如何将您标记为已回答的评论?
  • 我重新写了一个答案的评论,如果答案解决了你的问题,你可以点击答案旁边的勾号。如果您的问题仍然得到解答,您可以编辑您的问题以获得更多/更好的答案。

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


【解决方案1】:

经评论确认后:

您忘记包含#include&lt;algorithm&gt;。包含所需的标头以使符号 foreach 包含在 std 命名空间中,从而对程序的其余部分可见。

【讨论】:

    【解决方案2】:

    您的解决方案纯粹是 C++98,可能的 C++11 解决方案如下所示:

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

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

    【讨论】:

    • A C++11 解决方案”,而不是“ C++11 解决方案”;)
    猜你喜欢
    • 2019-03-29
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-22
    • 2013-02-03
    • 2010-11-29
    相关资源
    最近更新 更多