How to initialize a static std::map in a cpp file?

Hello++

I'm trying to initialize a static std::map in a cpp file. I looked at https://stackoverflow.com/questions/138600/initializing-a-static-stdmapint-int-in-c and tried to add:

#include <map>
static std::map<int, char> x = {{1, 'a'}, {2, 'b'}};

but get error while compiling the file:

error: invalid declarator before ‘x’
static std::map<int, char> x = {{1, 'a'}, {2, 'b'}};

So is the answer on stackoverflow incorrect? What is the proper way of doing it?

Thanx in advance.
The code is fine, however it needs a C++11 compiler.
What do you use ?
I solved it - it was just a typo by me done in a header file.

It turned out that I had included a class definition file before the "static..." and the class definition was not terminated by a ;

I'm using g++ with -std=c++11

Thanks for replying.
A variable can only be declared as static (in the header). You need to define it afterwards (without static) and then you may initialize it.

[EDIT]
This applies for the global scope only. In the local scope it is defined as well.
Last edited on
Topic archived. No new replies allowed.