Undefined Reference Error for Map

U

utab

Dear all,

I am getting an undefined reference error wrn I try to initilialize a
static map in the class constructor.
my map:
map<string,vector<string> > m;

and in the constructor

classname::classname(){

m["g"].push_back("x")
m["g"].push_back("y")
..
..
..
}

But compiling results in

undefined reference to `Classname::m'

I could not figure that out. (I think I ahve problems with static
initialization of a map inside the constructor)

Regards,
 
R

Rolf Magnus

utab said:
Dear all,

I am getting an undefined reference error wrn I try to initilialize a
static map in the class constructor.

You can't initialize static member variables in a constructor. The
constructor is called whenever an instance of your class is created, but a
static member variable doesn't belong to a specific instance.
my map:
map<string,vector<string> > m;

and in the constructor

classname::classname(){

m["g"].push_back("x")
m["g"].push_back("y")
.
.
.
}

But compiling results in

undefined reference to `Classname::m'

Did you define that object anywhere?
 
U

utab

You can't initialize static member variables in a constructor. The
constructor is called whenever an instance of your class is created, but a
static member variable doesn't belong to a specific instance.

I added another function to my class such as initialize

but still i get an undefined reference error
Did you define that object anywhere?

You mean the map, I did not get this point completely

Regards
 
M

mlimber

Rolf said:
You can't initialize static member variables in a constructor. The
constructor is called whenever an instance of your class is created, but a
static member variable doesn't belong to a specific instance.

While the static member must be initialized (perhaps default
constructed) outside the constructor, one can modify static members in
the constructor, as the OP is trying to do here. For instance, this is
legal and useful:

struct A
{
static int count_;
A() { ++count_; }
~A() { --count_; }
};

int A::count_ = 0; // Init

Cheers! --M
 
M

mlimber

utab said:
I added another function to my class such as initialize

That is unnecessary based on what you are trying to do. See my previous
post in this thread.
You mean the map, I did not get this point completely

When you declare a static member variable, you must define it outside
the class like this:

class A { static std::map<int,int> m_; };
std::map<int,int> A::m_;

Cheers! --M
 
M

mlimber

mlimber said:
That is unnecessary based on what you are trying to do.

N.B., using the constructor to do the initialization, your map's
vectors will be appended every time you create an instance of the
class. That may not be what you want. You might consider initializing
it when you define it using an initializer similar to the one given for
vector here:

http://groups.google.com/group/comp.lang.c++/msg/e2fe5982913d4414

Or simply add a static bool to your class that indicates whether or not
initialization of the map should be performed.

Cheers! --M
 
U

utab

Thank you for your interest and replies

Can I use a static function to reach the map and initialize that?

Regards,
 
M

mlimber

utab said:
Thank you for your interest and replies

Can I use a static function to reach the map and initialize that?

Please quote the message you are replying to so that those not using
Google Groups can follow the conversation more easily. To automatically
quote in Google Groups, click "show options" and then "Reply" in the
message header.

To answer your question: yes, but you'll have to call that function
from somewhere -- probably either by calling it in the constructor or
by putting the burden to call it on the user (not a good idea; you know
how forgetful users are).

Cheers! --M
 

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,780
Messages
2,569,611
Members
45,281
Latest member
Pedroaciny

Latest Threads

Top