Compilation error with “for each” loop in C++ VS2010

后端 未结 6 803
无人及你
无人及你 2021-01-21 10:29

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

相关标签:
6条回答
  • 2021-01-21 10:34

    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.

    0 讨论(0)
  • 2021-01-21 10:34

    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

    0 讨论(0)
  • 2021-01-21 10:40

    Foreach loop was introduced in the C++11 standard, the compilers at your school probably aren't up to date with the new standard.

    0 讨论(0)
  • 2021-01-21 10:42

    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.

    0 讨论(0)
  • 2021-01-21 10:42

    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#.

    0 讨论(0)
  • 2021-01-21 11:00

    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.

    0 讨论(0)
提交回复
热议问题