views:

463

answers:

2

Hey guys,

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.

+8  A: 

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

Alex Martelli
Yup, I just realized that and it fixed the issue.
Justin
using namespace std! Come on!
DanDan
using namespace std is evil - who knows when you'll get a name collision?
bdonlan
Why wouldn't ADL work here, unless the iterator for 'messages' isn't in the std namespace?
Fred Larson
+11  A: 

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

Sorantis