trouble using lambda expressions and auto keyword in c++ <p>I'm trying to learn how to use lambda expressions in C++.</p> <p>I tried this simple bit of code but I get compile errors:</p> <pre><code>int main() { vector&lt;int&gt; vec; for(int i = 1; i&lt;10; i++) { vec.push_back(i); } for_each(vec.begin(),vec.end(),[](int n){cout &lt;&lt; n &lt;&lt; " ";}); cout &lt;&lt; endl; } </code></pre> <p>Errors:</p> <pre><code> forEachTests.cpp:20:61: error: no matching function for call to'for_each(std::vector&lt;int&gt;::iterator, std::vector&lt;int&gt;::iterator, main()::&lt;lambda(int)&gt;)' forEachTests.cpp:20:61: note: candidate is: c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_algo.h:4373:5: note:template&lt;class _IIter, class _Funct&gt; _Funct std::for_each(_IIter, _IIter, _Funct) </code></pre> <p>I also tried making the lambda expression an auto variable but I got a different set of errors.</p> <p>Here is the code:</p> <pre><code>int main() { vector&lt;int&gt; vec; for(int i = 1; i&lt;10; i++) { vec.push_back(i); } auto print = [](int n){cout &lt;&lt; n &lt;&lt; " ";}; for_each(vec.begin(),vec.end(),print); cout &lt;&lt; endl; } </code></pre> <p>This gave me the following errors:</p> <pre><code> forEachTests.cpp: In function 'int main()': forEachTests.cpp:20:7: error: 'print' does not name a type forEachTests.cpp:22:33: error: 'print' was not declared in this scope </code></pre> <p>I'm assuming these are problems with my compiler but I am not quite sure. I just installed MinGW and it appears to be using <code>gcc</code> 4.6.2.</p>