Static Map initialized with Static Function but error

Discussion in 'C++' started by utab, May 15, 2006.

  1. utab

    utab Guest

    Dear all,

    I had some replies on my previous post.But wanted to learn sth. Static
    member functions of a class can access only the static data members of
    the same class. So this principle in mind, lets advance one step
    further,

    lets say in a class I have a map in the private declerations of the
    class

    private:
    static map<string, vector<string> > m_;

    and in the public part

    public:

    static void Initialize_();

    And in the implementation file of the class, inside the Initialize_()
    function I am trying to initialize the static map like

    m_["g"].push_back("x");
    m_["g"].push_back("x");
    m_["g"].push_back("z");

    This looks logically true to me but compilation does not agree with
    that. It results in an undefined reference error.

    And for nearly one day, I have not been able to figure out the problem.

    Regards,
     
    utab, May 15, 2006
    #1
    1. Advertising

  2. utab wrote:

    > Static
    > member functions of a class can access only the static data members of
    > the same class. So this principle in mind, lets advance one step
    > further,
    >
    > lets say in a class I have a map in the private declerations of the
    > class
    >
    > private:
    > static map<string, vector<string> > m_;
    >
    > and in the public part
    >
    > public:
    >
    > static void Initialize_();
    >
    > And in the implementation file of the class, inside the Initialize_()
    > function I am trying to initialize the static map like
    >
    > m_["g"].push_back("x");
    > m_["g"].push_back("x");
    > m_["g"].push_back("z");


    std::map does not have a push_back member function, so the above code
    is a syntax error. What is it you are trying to do? You could try:

    m_["g"] = "x";

    But I'm not sure that's what you're trying to do.

    > This looks logically true to me but compilation does not agree with
    > that. It results in an undefined reference error.


    In the future, you would make our lives much easier if you followed FAQ
    5.8:

    http://www.parashift.com/c -faq-lite/how-to-post.html#faq-5.8

    In particular, post compilable code and post the complete text of the
    error message you get.

    In addition to the above error, make sure you consult FAQ section
    10.11:

    http://www.parashift.com/c -faq-lite/ctors.html#faq-10.11

    Best regards,

    Tom
     
    Thomas Tutone, May 16, 2006
    #2
    1. Advertising

  3. utab

    Dennis Jones Guest

    "utab" <> wrote in message
    news:...
    > Dear all,
    >
    > I had some replies on my previous post.But wanted to learn sth. Static
    > member functions of a class can access only the static data members of
    > the same class. So this principle in mind, lets advance one step
    > further,
    >
    > lets say in a class I have a map in the private declerations of the
    > class
    >
    > private:
    > static map<string, vector<string> > m_;
    >
    > and in the public part
    >
    > public:
    >
    > static void Initialize_();
    >
    > And in the implementation file of the class, inside the Initialize_()
    > function I am trying to initialize the static map like
    >
    > m_["g"].push_back("x");
    > m_["g"].push_back("x");
    > m_["g"].push_back("z");
    >
    > This looks logically true to me but compilation does not agree with
    > that. It results in an undefined reference error.


    That's correct because, although you have declared 'm_', you have not
    defined it (class-static data members must be defined). In your
    implementation file, add something like:

    map<string, vector<string> > <classname>::m_;

    ....where <classname> is the name of your class (which you didn't give
    above).

    - Dennis
     
    Dennis Jones, May 16, 2006
    #3
  4. utab

    Dennis Jones Guest

    "Thomas Tutone" <> wrote in message
    news:...
    > utab wrote:
    >
    > std::map does not have a push_back member function, so the above code
    > is a syntax error. What is it you are trying to do? You could try:


    No, but a vector does, and the data type of the second template parameter in
    his map declaration is a vector. Thus, given:

    map<string, vector<string> > m_;

    ....then

    m_[ "somestring" ]

    ....refers to a vector

    - Dennis
     
    Dennis Jones, May 16, 2006
    #4
  5. Thomas Tutone wrote:
    > utab wrote:
    >
    > > Static
    > > member functions of a class can access only the static data members of
    > > the same class. So this principle in mind, lets advance one step
    > > further,
    > >
    > > lets say in a class I have a map in the private declerations of the
    > > class
    > >
    > > private:
    > > static map<string, vector<string> > m_;
    > >
    > > and in the public part
    > >
    > > public:
    > >
    > > static void Initialize_();
    > >
    > > And in the implementation file of the class, inside the Initialize_()
    > > function I am trying to initialize the static map like
    > >
    > > m_["g"].push_back("x");
    > > m_["g"].push_back("x");
    > > m_["g"].push_back("z");

    >
    > std::map does not have a push_back member function, so the above code
    > is a syntax error.


    Oops - I was asleep at the wheel - I somehow missed that it was a
    std::map< std::string, std::vector< std::string> >. Sorry about that.


    > In addition to the above error, make sure you consult FAQ section
    > 10.11:
    >
    > http://www.parashift.com/c -faq-lite/ctors.html#faq-10.11


    That advice still stands.

    Best regards,

    Tom
     
    Thomas Tutone, May 16, 2006
    #5
  6. Dennis Jones wrote:
    > "Thomas Tutone" <> wrote in message
    > news:...
    > > utab wrote:
    > >
    > > std::map does not have a push_back member function, so the above code
    > > is a syntax error. What is it you are trying to do? You could try:

    >
    > No, but a vector does, and the data type of the second template parameter in
    > his map declaration is a vector. Thus, given:
    >
    > map<string, vector<string> > m_;
    >
    > ...then
    >
    > m_[ "somestring" ]
    >
    > ...refers to a vector



    Absolutely right - see my other post. And thanks for the correction.

    Best regards,

    Tom
     
    Thomas Tutone, May 16, 2006
    #6
    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. Ge  Cong
    Replies:
    2
    Views:
    281
    Alf P. Steinbach
    Sep 14, 2005
  2. Replies:
    4
    Views:
    643
    Jonathan Mcdougall
    Nov 18, 2005
  3. Marcus
    Replies:
    2
    Views:
    591
    Marcus
    Dec 9, 2005
  4. , India
    Replies:
    5
    Views:
    1,783
    chisholm
    Feb 15, 2011
  5. Immortal Nephi
    Replies:
    15
    Views:
    6,415
    Bo Persson
    Jul 23, 2010
Loading...

Share This Page