If you use the command g++-g-wall main.cpp Compile the following code:
/* file:main.cpp*/#include <stdio.h>int main () {int a[5] = {1, 2, 2, 5, 1};for (int i:a) {printf ("%d\n", a [i]);} return 0;}
Then g++ will prompt the following error:
main.cpp:In function ' int main () ': main.cpp:5:13:error:range-based ' for ' loops is not allowed in c++98 mode for (in T i:a) {
This means that the loop is not supported in c++98, because this is the c++11 new loop method.
So what if you have to compile it?
The following methods can be learned by ordering man g++ :
g++-g-wall-std=c++11 main.cpp
In addition to g++, GCC can support C11 in a similar way
Gcc-g-wall-std=c11 main.cpp
What should I do if I don't want to write this-std=c++11 this option every time?
Method Source: HTTP://STACKOVERFLOW.COM/QUESTIONS/16886591/HOW-DO-I-ENABLE-C11-IN-GCC
Method 1: Write Makefile
Method 2: Alias: Aliasg++11= "g++-std=c++11"
gcc/g++ How to support C11/C++11 standard compilation