error c++98

When I try to compile the example program in the tutorial for creating a range-based for loop, I get an error: 10 16 C:\Users\Public\Documents\C prog source\range based for loop.cpp [Error] range-based 'for' loops are not allowed in C++98 mode. Is this sample program supposed to work?
Try compiling it with g++ -std=c++11 or g++ -std=c++14
Last edited on
I am a rank beginner. Could you elaborate on
g++ -std=c++11?

where this should be placed, what each element does. Here is the code I am trying to make work.

// range-based for loop
#include <iostream>
#include <string>
using namespace std;

int main ()
{
string str {"Hello!"};
for (char c : str)
{
std::cout << "[" << c << "]";
}
std::cout << '\n';
}
How are you compiling your code? I had assumed you were using g++, but perhaps you are using another compiler?

Also, welcome to the forums! Please be sure to use code tags in the future by clicking the <> button to the right of the text box.
Shows just how ignorant I am. I did not realize that there was a g++. No I am using the compiler that came with dev-cplusplus compiler that I downloaded from the net. And I thought this forum was for Cplusplus users.
I thought this forum was for Cplusplus users.

Yes, this forum is for helping with learning C++ :)

Shows just how ignorant I am.

Everyone learns what a compiler is at some point once they begin their journey to becoming a programmer.

In dev-cplusplus:
Go to Tools -> Compiler Options -> "Compiler" tab

Check the checkbox labeled, "Add the following commands when calling the compiler" And add in the text entry box, "-std=c++11" or if that doesn't work "-std=C++0x"

Should be something like that anyway, I haven't had Dev C++ installed for many years, so I had to look at some screenshots on Google to remember.

(Shamelessly plagiarized from http://stackoverflow.com/questions/16951376/how-to-change-mode-from-c98-mode-in-dev-c-to-a-mode-that-supports-c0x-ran )
closed account (48T7M4Gy)
hautry
One aspect to also consider if you have the facilities available, is to use a more up to date and supported IDE rather than dev C++ such. as visual studio, code blocks, Xcode, Qt creator which are way ahead. Each of those wil virtually automatically give you the ability and choice to use the various forms of C++ mentioned elsewhere here. I realise some schools in different parts haven't updated from Dev so students are stuck.
Last edited on
https://sourceforge.net/projects/orwelldevcpp/
Last updated in 2015, and it is using TDM-GCC 4.9 :)
Go to project->options->Compiler->Code generation and choose GNU C++ 11
https://www.dropbox.com/s/w51m8qb9m8sbqvn/DevC%2B%2B.jpg?dl=0
Thanks to all. !nimble fingers finally got it entered correctly. All fixed.
Topic archived. No new replies allowed.