-1

I have recently downloaded MinGW into my computer but on using certain containers and iterators like unordered_map and auto it shows an unexpected error.

my code is as follows :

#include <bits/stdc++.h>
#include<unordered_map>
using namespace std;


int main()
{

    unordered_map<string, int> umap; 

    umap["GeeksforGeeks"] = 10; 
    umap["Practice"] = 20; 
    umap["Contribute"] = 30; 

    for (auto x : umap) 
      cout << x.first << " " << x.second << endl; 


    return 0;
}

it gives the following error :


C:\Users\naima\Documents\cpp>g++ -o try2 try2.cpp
In file included from C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++/unordered_map:35:0,
                 from try2.cpp:2:
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++/bits/c++0x_warning.h:32:2: error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler options.
 #error This file requires compiler and library support for the \
  ^
try2.cpp: In function 'int main()':
try2.cpp:9:5: error: 'unordered_map' was not declared in this scope
     unordered_map<string, int> umap;
     ^
try2.cpp:9:25: error: expected primary-expression before ',' token
     unordered_map<string, int> umap;
                         ^
try2.cpp:9:27: error: expected primary-expression before 'int'
     unordered_map<string, int> umap;
                           ^
try2.cpp:11:5: error: 'umap' was not declared in this scope
     umap["GeeksforGeeks"] = 10;
     ^
try2.cpp:15:15: error: 'x' does not name a type
     for (auto x : umap)
               ^
try2.cpp:19:5: error: expected ';' before 'return'
     return 0;
     ^
try2.cpp:19:5: error: expected primary-expression before 'return'
try2.cpp:19:5: error: expected ';' before 'return'
try2.cpp:19:5: error: expected primary-expression before 'return'
try2.cpp:19:5: error: expected ')' before 'return'
Roomi
  • 139
  • 8
  • 4
    `This file requires compiler and library support for the ISO C++ 2011 standard.`. Range based loops require C++11. You need to compile with `-std=c++11` (or upgrade the compiler to at least GCC 6.1) – Yksisarvinen Nov 06 '19 at 15:30
  • 2
    `#include `: [Don't include that.](https://stackoverflow.com/Questions/31816095/Why-Should-I-Not-Include-Bits-Stdc-H.). Using `#include` also indicates that you don't know what it actually does. – walnut Nov 06 '19 at 15:34
  • Possible duplicate of [C++ error: 'unordered\_map' does not name a type](https://stackoverflow.com/questions/15733163/c-error-unordered-map-does-not-name-a-type) – walnut Nov 06 '19 at 15:42
  • @uneven_mark Thank you for feedback but I obviously know what bits/stdc++ does. I have included unordered_map header file to indicate that its the reason behind the above error. Even though I have g++ version 5.1 I still cant include c++11 features. – Roomi Nov 06 '19 at 15:59
  • @NaimaFarooqi Except for the `#error This file requires compiler and library support` error you will get the same result without `#include`. When you use `-std=c++11`, then `#include` will be redundant with `#include` (though as I wrote, you should stay with the former rather than the latter). (If you are aware of this, ignore my comment.) – walnut Nov 06 '19 at 16:01

1 Answers1

0

The compiler told you exactly what was wrong. It usually will.

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

You just have to compile with the proper flag, -std=c++11. I don't know if you are version-matching against what graders use or what, but there are very few good reasons to be on a minGW compiler where support for an 8 year old standard is still considered experimental.

You can see that it works as expected here: https://godbolt.org/z/JQxL00 If you remove the -std=c++11 flag, it will fail to compile and give you the same error message.

You might also notice that I altered the includes to only include what I use. This results in a much faster compile time, smaller executable, and an easier to understand piece of code (Since it is plain to see what standard features are being used). You also avoid polluting your namespace.

sweenish
  • 4,793
  • 3
  • 12
  • 23