initializing static const map

Discussion in 'C++' started by Marc D, Feb 14, 2007.

  1. Marc D

    Marc D Guest

    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
    */


    --
    Marc D.
    (note that reply-to is a black hole.]
    Use list [at] ryuu in Canada to send email.
    Or, preferably, reply here...
    Marc D, Feb 14, 2007
    #1
    1. Advertising

  2. Marc D

    Ian Collins Guest

    Marc D wrote:
    > 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!

    --
    Ian Collins.
    Ian Collins, Feb 14, 2007
    #2
    1. Advertising

  3. Marc D

    Marc D Guest

    Ian Collins wrote:
    > Marc D wrote:

    [...]
    >> }
    >>

    > Missing semicolon!
    >


    ::beats head against large brick wall::

    Thanks for that! You have no idea how long I've
    looked at the original problem, and this reduced
    set...

    need sleep.


    --
    Marc D.
    Marc D, Feb 14, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steven T. Hatton
    Replies:
    1
    Views:
    6,618
    Siemel Naran
    Apr 19, 2004
  2. Rakesh Sinha
    Replies:
    4
    Views:
    1,838
    Rakesh Sinha
    Jan 13, 2005
  3. Dave
    Replies:
    10
    Views:
    35,236
    Ron Natalie
    May 22, 2005
  4. Javier
    Replies:
    2
    Views:
    542
    James Kanze
    Sep 4, 2007
  5. er
    Replies:
    3
    Views:
    373
Loading...

Share This Page