473,396 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Undefined Reference Error for Map

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,

May 15 '06 #1
8 9716
utab wrote:
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?

May 15 '06 #2
Yes, that is true and very logical.

Thanks

May 15 '06 #3
> 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

May 15 '06 #4
Rolf Magnus wrote:
utab wrote:
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.


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

May 15 '06 #5
utab wrote:
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


That is unnecessary based on what you are trying to do. See my previous
post in this thread.
Did you define that object anywhere?


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

May 15 '06 #6
mlimber wrote:
utab wrote:
I added another function to my class such as initialize


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....fe5982913d4414

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

Cheers! --M

May 15 '06 #7
Thank you for your interest and replies

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

Regards,

May 15 '06 #8
utab wrote:
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

May 15 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: RU | last post by:
Hi, I am working on a porting project to port C/C++ application from unixware C++, AT&T Standard components to g++ with STL on Linux. This application has been working properly on...
1
by: Codemutant | last post by:
**** Post for FREE via your newsreader at post.usenet.com **** I just cannot find what is undefined in this code.
1
by: Foolster41 | last post by:
I'm rather new to C++ programing. I'm using the dev-C++ program on a windows XP OS. I'm trying to compile the code for a multi user dungeon (MUD) called circle-mud. When I compile I get the...
4
by: Chris Beall | last post by:
If you want your code to be bulletproof, do you have to explicitly check for the existence of any possibly-undefined variable? Example: window.outerHeight is defined by some browsers, but not...
1
by: Dom | last post by:
I'm new to c++. Just started learning it 24 hours ago. Am running into a compile problem. Please, no one waste the effort telling me to google it. I've been researching it for quite a while with no...
10
by: siroregano | last post by:
Hello- I've got a nice C program written that uses libsndfile (#include <sndfile.h>) to convert my raw data into a properly-formatted wav file. The program is composed of a single .c file that...
17
by: yb | last post by:
Hi, Looking for clarification of undefined variables vs. error in JavaScript code. e.g. <script> alert( z ); // this will be an error, i.e. an exception </script>
8
by: pavan734 | last post by:
Hello, Please excuse me as Iam not posting this to correct group. I have a parser code obtained from flex command. I have many other files. When I compile them Iam getting a message like:...
3
by: prakash.mirji | last post by:
Hello, I am getting below mention linker error when I tried to link my class test.C I use below command to compile test.C /usr/bin/g++ -g -fpic -fvisibility=default -D_POSIX_SOURCE...
2
by: zqiang320 | last post by:
Hello: I execute make ,then get error: $ make Making all in libsbml/src make: Entering directory `/home/internet/mydoc/test_pj/libsbml/src' ........ /bin/sh ./libtool --tag=CC --mode=link...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.