using map with my own objects

B

Bryan Pietrzak

I'm new to using the STL and templates and while I've had no problem
at all using vector, I'm having a problem using map.

I just want to do something like:

class CMyData
{
// class stuff here
};

typedef map<string, CMyData &> MyDataMap;

MyDataMap datamap;

datamap.insert(MyDataMap::value_type("some key", someDataReference));

But it won't compile. I have no problem using maps with the key and
data being built-in or STL types, but not with my own types.

I'm clearly missing something here, and I suspect it's related to the
compare function, but I'm really not sure what I'm supposed to be
doing.

Googling has not been very helpful, nor is the STL book I have as it
sticks to the well known types.

A little nudge in the right direction is all I'm looking for here...
:)
Bryan
 
J

Jeff Schwab

Bryan said:
I'm new to using the STL and templates and while I've had no problem
at all using vector, I'm having a problem using map.

I just want to do something like:

class CMyData
{
// class stuff here
};

typedef map<string, CMyData &> MyDataMap;

MyDataMap datamap;

datamap.insert(MyDataMap::value_type("some key", someDataReference));

But it won't compile. I have no problem using maps with the key and
data being built-in or STL types, but not with my own types.

I'm clearly missing something here, and I suspect it's related to the
compare function, but I'm really not sure what I'm supposed to be
doing.

Googling has not been very helpful, nor is the STL book I have as it
sticks to the well known types.

A little nudge in the right direction is all I'm looking for here...
:)
Bryan

Don't make the type of the value a reference. I.e., your type should
look like this:

typedef map< string, CMyData > MyDataMap;

Good luck!
 
J

John Harrison

typedef map<string, CMyData &> MyDataMap;
[snip]

Don't make the type of the value a reference. I.e., your type should
look like this:

typedef map< string, CMyData > MyDataMap;

Or a pointer, like this

typedef map< string, CMyData* > MyDataMap;

depending on exactly what the OP is trying to do.

john
 

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

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,142
Latest member
DewittMill
Top