Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

The code from my professor's homework assignment is as follows. This is straight out of the box, I have not modified my professor's code. There is more to the program, but this is where the error happens. Problem line in bold.

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

I receive the following error.

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

To summarize it, it's exactly like the title. "'For each' is not a member of std" I have already enabled standard 11 support in all the IDE's I have compiled it in. On code blocks I put the argument in the compiler settings, in netbeans I changed it under project properties on the compiler, even the linux system on the school's network won't run it, with the same error, and I used the standard 11 inclusion line.

Ideas on how to fix this, anyone? It used to give me the same error for all locations.

like image 874
user3424549 Avatar asked Jun 08 '15 10:06

user3424549


2 Answers

After being confirmed from the comment:

You forgot to include #include<algorithm>. Include the required header to make the symbol foreach included in std namespace and thus visible for the rest of the program.

like image 194
Mohit Jain Avatar answered Nov 01 '22 09:11

Mohit Jain


Your solution is purely C++98, a possible C++11 solution would look like that:

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

I like this way a lot more and it doesn't need the algorithm header.

like image 32
Klemens Morgenstern Avatar answered Nov 01 '22 08:11

Klemens Morgenstern