I was working on a little C++ project at home, which I brought into school to show my teacher. At home I have Visual Studio 2012, whereas the school computers have Visual Studio
The "range for" is a C++ 11 feature that was added in Visual Studio 2012. To learn more about which C++ 11 features are in Visual Studio 2008 (VC9) and Visual Studio 2010 (VC10), check the blog entry from the Visual C++ team. There are similar tables to let you know about Visual Studio 2012 and several different releases of Visual Studio 2013.
Bottom line: your for
loop that you did at home is great if you have Visual Studio 2012. If you don't, use a regular for
or std::for_each
, not the for each
you're using there.
The for(char c: myStr) syntax is one of the new C++11 features and VC++ in Visual Studio does not support it.
See this for a list of C++ features which VS2010 and VS2012 C++ compilers implement: http://blogs.msdn.com/b/vcblog/archive/2011/09/12/10209291.aspx
Foreach loop was introduced in the C++11 standard, the compilers at your school probably aren't up to date with the new standard.
C++11 range-based for loops aren't supported in Visual Studio 2010.
The second form is a syntax that leaked into the compiler from C++/CLI (an entirely different language that targets the .NET runtime). I filled a bug on this a while back. If you compile with the /Za
switch, it will disable this language extension. You will need to use the C++03 for loop syntax using an iterator or std::for_each
.
MS VC++ 2010 was released before the C++ 2011 Standard was adopted. So it does not support the range-based for statement. On the other hand MS VC++ 2010 has MS language extension for-each-in that was introduced in managed C++ to support foreach statement of C#.
The for(char c : myStr){...}
syntax is new with C++11
, so anything using an older version of C++
won't compile with that syntax.
Previous to C++11, for_each
is defined in the algorithm
header.