123456789101112 |
/*
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:
123 |
main.cpp: In function ‘ int main()’: main.cpp:5:13: error: range-based ‘ for ’ loops are not allowed in C++98 mode for ( int 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: Alias g++11= "g++-std=c++11"
"Go" gcc/g++ how to support C11/C++11 standard compilation