5

I wanted to use the unordered_map STL in c++, but as soon as I use the header, it gives me this error:

This file requires support for the compiler and library support for the ISO C++11 standard. This support is currently experimental and must be enabled with -std=c++11 or -std=gnu++11 compiler options.

I am attaching my code that I wanted to run, below. (Any inputs on the code are welcome too. thanks)

#include <iostream>
#include <unordered_map>
using namespace std;

class Node
{
public:
string a,b;
Node()
{
    a="hello";
    b="world";
}
};


int main ()
{
    unordered_map<Node> mymap;
    Node mynode;
    mymap.insert(mynode);
    std::cout << "myrecipe contains:" << std::endl;
  for (auto& x: mymap)
    std::cout << x.a << ": " << x.b << std::endl;

}

Edit: I got it to work by using the following commmand: g++ -std=c++11 [filename].cpp

Thanks for the help.

pyroscepter
  • 205
  • 1
  • 3
  • 9
  • "This support is currently experimental and must be enabled with -std=c++11 or -std=gnu++11 compiler options." What about that is not clear to you? Note that this is _compiler_ specific, and doesn't really have anything to do with your OS (presumably you're using gcc, which is also available on windows and OSX and a couple of other platforms IIRC). – Cubic Nov 16 '14 at 05:21
  • The question is how to get my code to compile without this error? thanks in advance. – pyroscepter Nov 16 '14 at 05:28
  • The error message contains the relevant instructions - just add one of the options (`-std=c++11` or `-std=gnu++11`) when compiling. – Cubic Nov 16 '14 at 05:47
  • [refer](https://stackoverflow.com/questions/17378969/how-to-change-gcc-compiler-to-c11-on-ubuntu) – pradeexsu Oct 15 '20 at 12:48

3 Answers3

8

The main answer to your question: specify -std=c++11 in your compile command.

Precisely which C++11 features are available will depend on your version of GCC. Here are two links that might help:

FoggyDay
  • 11,962
  • 4
  • 34
  • 48
3

First Option:

You can remove to error with -std=c++11 in compile time.

g++ -o binary yourFile.cpp -std=c++11

Second Option to integrate the development with c++11:

You can use a makefile with the CXXFLAGS set with -std=c++11 A makefile is a simple text file with instructions about how to compile your program. Create a new file named Makefile (with a capital M). To automatically compile your code just type the make command in a terminal. You may have to install make.

Here is the code :

CXX=clang++
CXXFLAGS=-g -std=c++11 -Wall -pedantic
BIN=prog

SRC=$(wildcard *.cpp)
OBJ=$(SRC:%.cpp=%.o)

all: $(OBJ)
    $(CXX) -o $(BIN) $^

%.o: %.c
    $(CXX) $@ -c $<

clean:
    rm -f *.o
    rm $(BIN)

It assumes that all the .cpp files are in the same directory as the makefile. But you can easily tweak your makefile to support a src, include and build directories.

Arnab Nandy
  • 6,472
  • 5
  • 44
  • 50
0

compile with:

g++ -o binary source.cpp -std=c++11
Rupesh Yadav.
  • 894
  • 2
  • 10
  • 24