Error: Range-based loops are not allowed in C++98

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// 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';
}


Why does this code give me 2 errors, 1 saying "Error: Range-based loops are not allowed in C++98" and another saying "in C++98 'str' must be initialised by contrusctor, not by '{...}""

I have tried installing tdm-gcc and searching the web, no specific answers. What can I do to fix this?
Last edited on
you should enable -std=c++11 when compiling
eg :
g++ main.cpp -std=c++11


make sure though that the compiler supports C++11 by:
g++ --version

4.7 or higher should be fine (i think)
Sorry if this is stupid, but can you please tell me how to do this?
Are you using an IDE ?
Yes, I am using Code::Blocks version 12.11.
Right click your project in the left pane -> build options -> compiler settings -> check the box "enable support for c++ 11 standard" or something similar
Last edited on
Thank you so much!
Glad to help !
Topic archived. No new replies allowed.