Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Static Map initialized with Static Function but error

Reply
Thread Tools

Static Map initialized with Static Function but error

 
 
utab
Guest
Posts: n/a
 
      05-15-2006
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,

 
Reply With Quote
 
 
 
 
Thomas Tutone
Guest
Posts: n/a
 
      05-15-2006
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-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

 
Reply With Quote
 
 
 
 
Dennis Jones
Guest
Posts: n/a
 
      05-15-2006

"utab" <> wrote in message
news: ups.com...
> 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


 
Reply With Quote
 
Dennis Jones
Guest
Posts: n/a
 
      05-15-2006

"Thomas Tutone" <> wrote in message
news: oups.com...
> 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


 
Reply With Quote
 
Thomas Tutone
Guest
Posts: n/a
 
      05-15-2006

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-lit...html#faq-10.11


That advice still stands.

Best regards,

Tom

 
Reply With Quote
 
Thomas Tutone
Guest
Posts: n/a
 
      05-15-2006

Dennis Jones wrote:
> "Thomas Tutone" <> wrote in message
> news: oups.com...
> > 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Integral const static member initialized inside a class subramanian100in@yahoo.com, India C++ 5 02-15-2011 07:15 PM
Static const integral data members can be initialized? Immortal Nephi C++ 15 07-23-2010 06:07 PM
STL: Map of maps possible, but no multi-map of maps? Workarounds? Marcus C++ 2 12-09-2005 06:34 AM
Are new std::map elements initialized? int2str@gmail.com C++ 4 11-18-2005 04:20 AM
call static method when class initialized Ge Cong C++ 2 09-14-2005 06:11 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57