Static Map initialized with Static Function but error

U

utab

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,
 
T

Thomas Tutone

utab said:
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
 
D

Dennis Jones

utab said:
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
 
D

Dennis Jones

Thomas Tutone said:
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
 
T

Thomas Tutone

Thomas said:
utab said:
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
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
 
T

Thomas Tutone

Dennis said:
Thomas Tutone said:
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
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top