Notice that no type is mentioned anywhere. Iterators don't provide any information on the type that their containers hold, so if the function doesn't provide any type information, either, the compiler is left with an std::for_each() that it doesn't know how to generate code for.
Basically, it's a mutual dependency problem. Your template function depends on the types that std::for_each() will use, and std::for_each() depends on the type of parameter that your function will take.
You can break the dependency by explicitly telling the compiler to make an instance of the template function, however: for_each(v.begin(), v.end(), squared<int>);