Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

for_each() in C++

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.

like image 718
Justin Avatar asked Aug 06 '09 15:08

Justin


2 Answers

try std::for_each() instead. Perhaps it can't see the namespace.

like image 137
Sorantis Avatar answered Oct 23 '22 13:10

Sorantis


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;-).

like image 33
Alex Martelli Avatar answered Oct 23 '22 15:10

Alex Martelli