initializing static const map

M

Marc D

OK. I've seen several posts on the subject in
the archives, but I still seem to be stuck.
Perhaps someone with fresher, more knowledgeable
eyes can tell me what I'm doing wrong?

I'm getting the error:
mapinit.cpp:15: error: expected initializer before 'A'
when I try to compile the following, and for the
life of me I can't figure out WHY. Anyone?
(I've tried several variations of this, including
declaring _init_mapconst() as const, and not using
inline, etc. Same error each time.)

Any assistance welcome.
thanks,
Marc



// minimal example of map initialization problem:

#include <string>
#include <map>

class A {
private:
typedef std::map<std::string, int> maptype_t;

static const maptype_t _mapconst;
static maptype_t _init_mapconst();
}

inline A::maptype_t
A::_init_mapconst() // line 15
{
maptype_t m;
m["aaa"] = 23;
m["acb"] = 42;
return m;
}

const A::maptype_t
A::_mapconst =
A::_init_mapconst();

int main()
{
}

/* compilation attempt:
$$> g++ -Wall -pedantic -ansi mapinit.cpp -o mapinit
mapinit.cpp:15: error: expected initializer before 'A'

$$> g++ --version
g++ (GCC) 4.0.2
*/
 
I

Ian Collins

Marc said:
OK. I've seen several posts on the subject in
the archives, but I still seem to be stuck.
Perhaps someone with fresher, more knowledgeable
eyes can tell me what I'm doing wrong?

I'm getting the error:
mapinit.cpp:15: error: expected initializer before 'A'
when I try to compile the following, and for the
life of me I can't figure out WHY. Anyone?
(I've tried several variations of this, including
declaring _init_mapconst() as const, and not using
inline, etc. Same error each time.)
class A {
private:
typedef std::map<std::string, int> maptype_t;

static const maptype_t _mapconst;
static maptype_t _init_mapconst();
}
Missing semicolon!
 

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top