std::map

This is my first time to use std::map and it wasn't very successful. Could somebody help with this code:

enum Units { Peasant, Archer, Footman, Griffon, Hero };

const int PeasantPrice = 30;
const int FootmanPrice = 90;
const int ArcherPrice = 50;
const int GriffonPrice = 150;

map <enum Units, int> UnitPrice = { { Peasant, PeasantPrice }, { Footman, FootmanPrice }, { Archer, ArcherPrice }, { Griffon, GriffonPrice } };

The error I got is:

error: could not convert '{{Peasant, PeasantPrice}, {Footman, FootmanPrice}, {Archer, ArcherPrice}, {Griffon, GriffonPrice}}' from '<brace-enclosed initializer list>' to 'std::map<Units, int>'|
The online compiler (http://cpp.sh/) accepts the code below in both C++11 and in C++14 mode. What is your compiler?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <map>

int main()
{
  enum Units { Peasant, Archer, Footman, Griffon, Hero };

  const int PeasantPrice = 30;
  const int FootmanPrice = 90;
  const int ArcherPrice = 50;
  const int GriffonPrice = 150;

  std::map<enum Units, int> UnitPrice = { { Peasant, PeasantPrice },
    { Footman, FootmanPrice },
    { Archer, ArcherPrice },
    { Griffon, GriffonPrice } };

  for ( auto x : UnitPrice ) {
    std::cout << x.first << ' ' << x.second << '\n';
  }
  return 0;
}
I tried in VS 2013 and CodeBlocks 13.12
But what the added from you code means?
VS and CB are IDE's. VS has Microsoft's C++ compiler. What compiler does your CodeBlocks use?


Do you mean the lines 18-20? That is just a loop to print the contents of the map. (The rest is from your post.)
Sorry, but I'm very beginner and I don't know even where to see what compiler CB uses. Could you help with that?
I have never seen CodeBlocks, but I do assume that it has documentation.
It uses C++98
C++98 is not a compiler. It is the first official C++ standard.

It seems that there are three different Windows installers for CB: http://www.codeblocks.org/downloads/26

Two of them do include TDM-GCC, which is a port of GCC (Gnu Compiler Collection). The third probably does not include any compiler, but does detect and offer to use any compilers that you already had.

The GCC is/has C++ compiler. The GCC does by default support C++98 (with Gnu extensions), but has command-line flag to support different version of standard. The CB should have means to set the flag.

The latest TDM-GCC seems to be version 4.9.2; newer than those included in CB's installer.
http://tdm-gcc.tdragon.net/download
The 4.9.2 has better C++14 -support than the 4.8.1 (but still defaults to the 98 standard).


Change the compiler options and/or change the compiler.
Topic archived. No new replies allowed.