M
Michael H Lees
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>:perator<(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
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>:perator<(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