C++ – for_each() in C++

ccompiler-errorsforeach

I've compiled my code on two different machines, which I thought had identical setups. However, one compiles without issues, and the other gives the following error.

LogEventReader.cpp(320) : error C3861: 'for_each': identifier not found, even with argument-dependent lookup

The relevant code:

#include <algorithm> 
...
for_each(messages.begin(), messages.end(), processXMLMessage);

Any ideas what the issue could be? TIA.

Best Answer

A likely issue is that the first compiler wants a using namespace std; before allowing the use of undecorated identifiers from that namespace (such as for_each), while the second one is over-permissive and doesn't demand it.

Of course, as other answers and comments hotly pointed out, there are probably-preferable alternatives, such as explicitly spelling it std::for_each at each occurrence, or employing a using declaration (using std::for_each;) instead of the broader using directive (using namespace std;) -- but this (good) advice is not a response to your question of why one compiler would diagnose an error while another did not;-).

Related Topic