Change C++ Mode

I am trying to compile a program using a new C++ feature but cannot,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

using namespace std;

int main() {

int a[10]={1,2,3,4,5,6,7,8,9,0};

for(int n : a){
        
        cout << n << " ";

        }

return 0;

}


I get the Compilation Error

range-based-for loops are not allowed in C++98 mode

How to compile with new mode, tried the "-ansi" command with compiler but no use. can some one tell me how can I use new features, The compiler does recognize them as far as error message is telling but how to tell compiler to compile using new modes (0x or 11). I am using DevC++ with MinGW 4.6.1 (latest I think).
Last edited on
Use the -std=c++0x option.

gcc only supports range for loops since version 4.6, so your 4.6.1 should be all right. Your program works for me on linux with gcc 4.6.2.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

using namespace std;

int main() {

int a[]={1,2,3,4,5};

for(int n : a){
        
        cout << n << " ";
        
        }

cout << "\nEnd\n";

return 0;

}


I could not do it with my IDE, so just used command line

gcc 9.cpp -std=c++0x

and many errors of undefined reference
At the very least the compiler is called g++, not gcc, from command line.

I use:
 ~ $ g++ -O3 -march=native -W -Wall  -Wextra -pedantic -std=c++0x -o test test.cc
 ~ $ ./test
1 2 3 4 5
End
Thank you,
After many efforts I decided to change my IDE, and it proved that problem was with my IDE (old version)

Command line also has some problem, not sure what but new IDE version did it. Also thanks for the commands, I now understand them better.
Just added these two commands for compiler commands

-pedantic -std=c++0x
Topic archived. No new replies allowed.