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

STL maps and const keys

Hi there,

I'm trying to use an stl map where I have my own defined (templated)
class as a key. I have the class "variableId" below which I want to use
as the key for an stl map. It has two members agent and slot. I've
defined the < operator but I'm slightly confused, here's the complete
class anyhow,

template <typename agentId, typename slotId>
class variableId
{
public:
agentId agent;
slotId slot;
variableId(){}
variableId(agentId aId, slotId sId);
bool operator<(variableId vId);
};

template <typename agentId, typename slotId>
variableId<agentId,slotId>::variableId(agentId aId, slotId sId)
{
agent=aId;
slot=sId;
}

template <typename agentId, typename slotId>
bool variableId<agentId,slotId>::
operator< (variableId vId)
{
return (slot<vId.slot && agent<vId.agent);
}
When I then try and do something like...

map<variableId<int,int>,int> m;
variableId<int,int> varname(1,1);
m[varname] = 1;
I get gcc reporting an error on the line m[varname]=1, something like

/usr/local/include/g++-v3/bits/stl_function.h:141: passing `const
variableId<int, int>' as `this' argument of `bool variableId<agentId,
slotId>::operator<(variableId<agentId, slotId>) [with agentId = int,
slotId = int]' discards qualifiers

I guess I'm either calling incorrecty or I've specified the
class/operator incorrectly.
Any help much appreciated
Thanks

ps. remove ++ from address to reply

Jul 19 '05 #1
3 12676
On Wed, 25 Jun 2003 16:49:02 +0100, Michael H Lees
<mh*@cs.nott.ac.uk++> wrote:
Hi there,

I'm trying to use an stl map where I have my own defined (templated)
class as a key. I have the class "variableId" below which I want to use
as the key for an stl map. It has two members agent and slot. I've
defined the < operator but I'm slightly confused, here's the complete
class anyhow,

template <typename agentId, typename slotId>
class variableId
{
public:
agentId agent;
slotId slot;
variableId(){}
variableId(agentId aId, slotId sId);
bool operator<(variableId vId);
Should be:
bool operator<(variableId vId) const;
or
bool operator<(variableId const& vId) const;
};

template <typename agentId, typename slotId>
variableId<agentId,slotId>::variableId(agentId aId, slotId sId)
{
agent=aId;
slot=sId;
}

template <typename agentId, typename slotId>
bool variableId<agentId,slotId>::
operator< (variableId vId)
{
return (slot<vId.slot && agent<vId.agent);
}
template <typename agentId, typename slotId>
bool variableId<agentId,slotId>::
operator< (variableId vId) const
{
return (slot<vId.slot && agent<vId.agent);
}

or

template <typename agentId, typename slotId>
bool variableId<agentId,slotId>::
operator< (variableId const& vId) const
{
return (slot<vId.slot && agent<vId.agent);
}



When I then try and do something like...

map<variableId<int,int>,int> m;
variableId<int,int> varname(1,1);
m[varname] = 1;
That's fine.


I get gcc reporting an error on the line m[varname]=1, something like

/usr/local/include/g++-v3/bits/stl_function.h:141: passing `const
variableId<int, int>' as `this' argument of `bool variableId<agentId,
slotId>::operator<(variableId<agentId, slotId>) [with agentId = int,
slotId = int]' discards qualifiers
Right, you need a const operator since the key in a map is const.

I guess I'm either calling incorrecty or I've specified the
class/operator incorrectly.


The latter.

Tom
Jul 19 '05 #2
Michael H Lees wrote:
Hi there,

I'm trying to use an stl map where I have my own defined (templated)
class as a key. I have the class "variableId" below which I want to use
as the key for an stl map. It has two members agent and slot. I've
defined the < operator but I'm slightly confused, here's the complete
class anyhow,

template <typename agentId, typename slotId>
class variableId
{
public:
agentId agent;
slotId slot;
variableId(){}
variableId(agentId aId, slotId sId);
bool operator<(variableId vId);
bool operator<(const variableId& vId) const;
};

template <typename agentId, typename slotId>
variableId<agentId,slotId>::variableId(agentId aId, slotId sId) : agent(aId), slot(sId) // prefer initialization lists.
{
agent=aId;
slot=sId;
}
The above assignments should be replaced by the
initialization list above.

template <typename agentId, typename slotId>
bool variableId<agentId,slotId>::
operator< (variableId vId)
operator< (const variableId& vId) const
{
return (slot<vId.slot && agent<vId.agent); // Perhaps some spaces would improve readability:
// (also, return is not a function and doesn't require
// an expression in parenthesis).
return (slot < vId.slot) && (agent < vId.agent);
}
When I then try and do something like...

map<variableId<int,int>,int> m;
variableId<int,int> varname(1,1);
m[varname] = 1;
I get gcc reporting an error on the line m[varname]=1, something like

/usr/local/include/g++-v3/bits/stl_function.h:141: passing `const
variableId<int, int>' as `this' argument of `bool variableId<agentId,
slotId>::operator<(variableId<agentId, slotId>) [with agentId = int,
slotId = int]' discards qualifiers

I guess I'm either calling incorrecty or I've specified the
class/operator incorrectly.
Any help much appreciated
Thanks

ps. remove ++ from address to reply


Add the "const" qualifiers to the "operator <" method above.
The key of a map is constant, but the comparison function wasn't.
--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Jul 19 '05 #3
Michael H Lees wrote:
Replying to my own question.... I know.

I managed to fix the problem by looking at stl::pair as an example.

I redefined the < operator as follows.....

template <class agentId, class slotId>
inline bool operator<(const variableId<agentId, slotId>& v1,
const variableId<agentId, slotId>& v2)
{
return (v1.slot<v2.slot && v1.agent<v2.agent);
}

I had tried all manner of things but missed the inline option. Should I
define all my operators as inline if I'm using the class as a key for a
map?

Cheers

_mike_

1. Don't top-post. Replies are either intermixed or appended to
the bottom of a post.
2. A rule of thumb of inlining: Inline simple functions. A get or
a set method is an example. If the overhead of the function
call is greater than the code in the function, then inline the
function.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Jul 19 '05 #4

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

Similar topics

4
by: Simon Elliott | last post by:
Here's a small example which uses std::maps within std::maps. Note the line flagged // *** copy? #include <iostream> #include <map> #include <string> struct Tfoo { Tfoo():i1_(0),i2_(0){}
43
by: Steven T. Hatton | last post by:
Now that I have a better grasp of the scope and capabilities of the C++ Standard Library, I understand that products such as Qt actually provide much of the same functionality through their own...
15
by: asterixgallier | last post by:
hello :) i have a simple (?) problem using maps with my own type for the key. i have used a struct overloading the < operator, but if i search (using find) with a key, that does not exists in...
5
by: DrLex | last post by:
This is a really annoying thing to look up in Google because all pages that mention STL maps or vectors will most likely also contain the word "template". So maybe this question has been asked...
3
by: jacek.dziedzic | last post by:
Hi! What is the canonical way of finding an intersection of two std::maps? i.e. I have std::map<whatever,size_tmap1; std::map<whatever,size_tmap2; .... and I need an std::vector...
6
by: Hicham Mouline | last post by:
Hello, I am attempting to design a class: class C { std::map<double, const Ama; std::map<double, const Bma; ..
9
by: fgh.vbn.rty | last post by:
I am frequently using maps like the following: map<string, map<int, vector< pair<int, int m1; map<int, map<string, map<int, int m2; This can be a little difficult to maintain if another coder...
15
by: brad | last post by:
Do maps convert to multimaps if the same key is inserted more than once?
19
by: C++Liliput | last post by:
I have a custom String class that contains an embedded char* member. The copy constructor, assignment operator etc. are all correctly defined. I need to create a map of my string (say a class...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.