There is nothing wrong with your code. The issue is that libstdc++ does not support std::for_each_n as of yet. If we look at header that defines std::for_each we see it does not exist.
However, if you have access to libc++, their header from the official mirror does implement std::for_each_n