Static Map

U

utab

Dear all,

In the private data section of my class I have defined a

static map<string, map<string,int> > Ent_fname_fid;

and in the implementation file I wanted to initialize it like a
built-in static variable
(if I gave a static int a in the class then I would have to write int
Classname::a=0)

This is not possible with the below code

map<string, map<string,int> > classname::Ent_fname_fid;

Ent_fname_fid["GRID"].insert(make_pair("GRID",1));
Ent_fname_fid["GRID"].insert(make_pair("ID",2));
Ent_fname_fid["GRID"].insert(make_pair("CP",3));
Ent_fname_fid["GRID"].insert(make_pair("X1",4));
Ent_fname_fid["GRID"].insert(make_pair("X2",5));
Ent_fname_fid["GRID"].insert(make_pair("X3",6));
Ent_fname_fid["GRID"].insert(make_pair("CD",7));
Ent_fname_fid["GRID"].insert(make_pair("PS",8));
Ent_fname_fid["GRID"].insert(make_pair("SEID",9));

This code is not in a function just outside all function definitions.

Regards,
 
V

Victor Bazarov

utab said:
In the private data section of my class I have defined a

static map<string, map<string,int> > Ent_fname_fid;

and in the implementation file I wanted to initialize it like a
built-in static variable
(if I gave a static int a in the class then I would have to write int
Classname::a=0)

This is not possible with the below code

map<string, map<string,int> > classname::Ent_fname_fid;

Ent_fname_fid["GRID"].insert(make_pair("GRID",1));
Ent_fname_fid["GRID"].insert(make_pair("ID",2));
Ent_fname_fid["GRID"].insert(make_pair("CP",3));
Ent_fname_fid["GRID"].insert(make_pair("X1",4));
Ent_fname_fid["GRID"].insert(make_pair("X2",5));
Ent_fname_fid["GRID"].insert(make_pair("X3",6));
Ent_fname_fid["GRID"].insert(make_pair("CD",7));
Ent_fname_fid["GRID"].insert(make_pair("PS",8));
Ent_fname_fid["GRID"].insert(make_pair("SEID",9));

This code is not in a function just outside all function definitions.


Correct. This is not possible. So, put it in a function and init
a dummy int variable with that function:

int dummy = function_to_fill_the_map(Ent_fname_fid);

The function should take one argument - a reference to non-const
map of yours. In the body of the function you will put all those
'insert' things. Return 0 from the function.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,276
Latest member
Sawatmakal

Latest Threads

Top