473,326 Members | 2,136 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,326 software developers and data experts.

using map with my own objects

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
Jul 22 '05 #1
2 1178
Bryan Pietrzak wrote:
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!

Jul 22 '05 #2
> >
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
Jul 22 '05 #3

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

Similar topics

40
by: Elijah Bailey | last post by:
I want to sort a set of records using STL's sort() function, but dont see an easy way to do it. I have a char *data; which has size mn bytes where m is size of the record and n is the...
1
by: Matt Alanzo | last post by:
On another newsgroup an Access knowledgable party posted: >You should be able to connect an Access ADP to an existing SQLExpress >database running in SQLS 2000 compatibility mode. The only thing...
19
by: Kamilche | last post by:
I have looked at many object-oriented programming frameworks out there for C. Though the ideas presented are intriguing, and I've used some of them in my own work, they all suffered some drawback...
5
by: Anders Borum | last post by:
Hello! Whilst refactoring an application, I was looking at optimizing a ModelFactory with generics. Unfortunately, the business objects created by the ModelFactory doesn't provide public...
13
by: Andy Baxter | last post by:
Can anyone recommend a good online guide to using objects in javascript? The book I bought (DHTML Utopia) suggests using objects to keep the code clean and stop namespace clashes between different...
1
by: Aaron West | last post by:
Try this script to see what queries are taking over a second. To get some real output, you need a long-running query. Here's one (estimated to take over an hour): PRINT GETDATE() select...
10
by: mg | last post by:
I'm migrating from VB6 and have a question about using 'Using' and the best way to use it. Here is a example of a small bit of code: dbConx("open") Using CN Dim CMD As New OleDbCommand(sSQL,...
2
by: Veloz | last post by:
Hiya My question is whether or not you should associated related objects in your software using a scheme of id's and lookups, or wether objects should actually hold actual object references to...
18
by: Angus | last post by:
Hello We have a lot of C++ code. And we need to now create a library which can be used from C and C++. Given that we have a lot of C++ code using classes how can we 'hide' the fact that it is...
1
by: bizt | last post by:
Hi, Im currently looking to move into using JSON for AJAX instead of returning from the server a string like the following: 12345{This is a text string{true I prefer objects because I dont...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.