Connecting Tech Pros Worldwide Forums | Help | Site Map

Static Map initialized with Static Function but error

utab
Guest
 
Posts: n/a
#1: May 15 '06
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,


Thomas Tutone
Guest
 
Posts: n/a
#2: May 16 '06

re: Static Map initialized with Static Function but error


utab wrote:
[color=blue]
> 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");[/color]

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.
[color=blue]
> This looks logically true to me but compilation does not agree with
> that. It results in an undefined reference error.[/color]

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

http://www.parashift.com/c++-faq-lit...t.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-lit...html#faq-10.11

Best regards,

Tom

Dennis Jones
Guest
 
Posts: n/a
#3: May 16 '06

re: Static Map initialized with Static Function but error



"utab" <umut.tabak@gmail.com> wrote in message
news:1147731134.202834.24990@i39g2000cwa.googlegro ups.com...[color=blue]
> 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.[/color]

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
Guest
 
Posts: n/a
#4: May 16 '06

re: Static Map initialized with Static Function but error



"Thomas Tutone" <Thomas8675309@yahoo.com> wrote in message
news:1147734600.811677.305370@j33g2000cwa.googlegr oups.com...[color=blue]
> 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:[/color]

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


Thomas Tutone
Guest
 
Posts: n/a
#5: May 16 '06

re: Static Map initialized with Static Function but error



Thomas Tutone wrote:[color=blue]
> utab wrote:
>[color=green]
> > 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");[/color]
>
> std::map does not have a push_back member function, so the above code
> is a syntax error.[/color]

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.

[color=blue]
> In addition to the above error, make sure you consult FAQ section
> 10.11:
>
> http://www.parashift.com/c++-faq-lit...html#faq-10.11[/color]

That advice still stands.

Best regards,

Tom

Thomas Tutone
Guest
 
Posts: n/a
#6: May 16 '06

re: Static Map initialized with Static Function but error



Dennis Jones wrote:[color=blue]
> "Thomas Tutone" <Thomas8675309@yahoo.com> wrote in message
> news:1147734600.811677.305370@j33g2000cwa.googlegr oups.com...[color=green]
> > 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:[/color]
>
> 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[/color]


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

Best regards,

Tom

Closed Thread