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

map whose key_type is the pointer to an object

Hi,

in the following piece of code I need to iterate through a vector to
create a map that has as key type the pointer to an object of the
UserEquipment class and as value type size_t.
The problem I encountered is that even if the address of the pointer to
the UserEquipment (in the code it is pActiveUe) changes inside the
cycle, the key doesn't seem to change because the previous value is
overwritten with the new value instead of a new element being added to
the map. Can anyone explain why?
Thanks.

vector<Connection*>& rpActiveConnections =
mpSector->GetrActiveConnections();
if ( !rpActiveConnections.empty() ) {
// in the header: map<UserEquipment*, size_t> mNumOfFreqSlotsToAdd;
mNumOfFreqSlotsToAdd.clear();
UserEquipment* pActiveUe;
double scalingFactor;
size_t numOfFreqSlotsToRemove;
for (vector<Connection*>::iterator activeConIt =
rpActiveConnections.begin();
activeConIt != rpActiveConnections.end(); ++activeConIt) {
pActiveUe = (*activeConIt)->GetpUserEquipment();
vector<int>& rAllocatedFreqSlotsForUe =
pActiveUe->GetrAllocatedFrequencySlots();
if ( pActiveUe->GetUplinkSinr_l() > mSinrTarget_l +
pActiveUe->GetPowerStep_l() ) {
scalingFactor = pActiveUe->GetUplinkSinr_l() /
mSinrTarget_l;
}
else {
scalingFactor =
pActiveUe->GetMaxTransmitPower_W()/pActiveUe->GetTransmitPower_W();
}
// the following instruction is my problem
mNumOfFreqSlotsToAdd[pActiveUe] =
(size_t)ceil( rAllocatedFreqSlotsForUe.size() *
(scalingFactor-1) );
cout << "mNumOfFreqSlotsToAdd has size "<<
mNumOfFreqSlotsToAdd.size() << endl;
}
}

Jan 17 '06 #1
4 1896
cesco wrote:
Hi,

in the following piece of code I need to iterate through a vector to
create a map that has as key type the pointer to an object of the
UserEquipment class and as value type size_t.
The problem I encountered is that even if the address of the pointer to
the UserEquipment (in the code it is pActiveUe) changes inside the
cycle, the key doesn't seem to change because the previous value is
overwritten with the new value instead of a new element being added to
the map. Can anyone explain why?
Thanks.

You haven't given us enough to run the code. Are you sure that
pActiveUe really is changing? I don't think the problem is with
map::operator[]. Try printing out the value of pActiveUe immediately
before that line.
vector<Connection*>& rpActiveConnections =
mpSector->GetrActiveConnections();
if ( !rpActiveConnections.empty() ) {
// in the header: map<UserEquipment*, size_t> mNumOfFreqSlotsToAdd;
mNumOfFreqSlotsToAdd.clear();
UserEquipment* pActiveUe;
double scalingFactor;
size_t numOfFreqSlotsToRemove;
for (vector<Connection*>::iterator activeConIt =
rpActiveConnections.begin();
activeConIt != rpActiveConnections.end(); ++activeConIt) {
pActiveUe = (*activeConIt)->GetpUserEquipment();
vector<int>& rAllocatedFreqSlotsForUe =
pActiveUe->GetrAllocatedFrequencySlots();
if ( pActiveUe->GetUplinkSinr_l() > mSinrTarget_l +
pActiveUe->GetPowerStep_l() ) {
scalingFactor = pActiveUe->GetUplinkSinr_l() /
mSinrTarget_l;
}
else {
scalingFactor =
pActiveUe->GetMaxTransmitPower_W()/pActiveUe->GetTransmitPower_W();
}
// the following instruction is my problem
mNumOfFreqSlotsToAdd[pActiveUe] =
(size_t)ceil( rAllocatedFreqSlotsForUe.size() *
(scalingFactor-1) );
cout << "mNumOfFreqSlotsToAdd has size "<<
mNumOfFreqSlotsToAdd.size() << endl;
}
}

Jan 17 '06 #2

cesco wrote:
Hi,

// the following instruction is my problem
mNumOfFreqSlotsToAdd[pActiveUe] =
(size_t)ceil( rAllocatedFreqSlotsForUe.size() *
(scalingFactor-1) );
cout << "mNumOfFreqSlotsToAdd has size "<<
mNumOfFreqSlotsToAdd.size() << endl;
}
}


Well having finally being able to decipher through your indenting style
(don't know why K&R is still so popular but even indenting K&R a bit of
whitespace wouldn't go amiss), it appears you are extending a class
(adding an extra size member) by having a map to all the instances of
your classes and mapping it to such a member. Well maybe not all the
instances.

So really I'm not sure your design is right.

Jan 17 '06 #3
Thanks for the reply.

Yes, I'm sure the address is changing. These are the addresses of
pActiveUe I get over four cycles of the same 'for' loop:

pActiveUe has address 006C89C0
pActiveUe has address 006CA8B0
pActiveUe has address 006D1F48
pActiveUe has address 006D4888

Any other idea?

Thanks

Jan 17 '06 #4
cesco wrote:
Thanks for the reply.

Yes, I'm sure the address is changing. These are the addresses of
pActiveUe I get over four cycles of the same 'for' loop:

pActiveUe has address 006C89C0
pActiveUe has address 006CA8B0
pActiveUe has address 006D1F48
pActiveUe has address 006D4888

Any other idea?

Thanks


Ideally, provide a compilable sample of code which exhibits the problem.

Beyond that, how do you know that the previous value is being
overwritten? Have you tried using the map::insert function whose pair
return value indicates whether a new item was actually inserted?
Jan 17 '06 #5

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

Similar topics

10
by: Chris Mantoulidis | last post by:
I see some really weird output from this program (compiled with GCC 3.3.2 under Linux). #include <iostream> using namespace std; int main() { char *s; s = "test1"; cout << "s = " << s << "...
110
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
204
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 =...
16
by: aegis | last post by:
Given the following: int a = 10; int *p; void *p1; unsigned char *p2; p = &a;
41
by: Alexei A. Frounze | last post by:
Seems like, to make sure that a pointer doesn't point to an object/function, NULL (or simply 0) is good enough for both kind of pointers, data pointers and function pointers as per 6.3.2.3: 3 An...
2
weaknessforcats
by: weaknessforcats | last post by:
Handle Classes Handle classes, also called Envelope or Cheshire Cat classes, are part of the Bridge design pattern. The objective of the Bridge pattern is to separate the abstraction from the...
5
by: pgrazaitis | last post by:
I cant seem to get my head wrapped around this issue, I have myself so twisted now there maybe no issue! Ok so I designed a class X that has a few members, and for arguments sake one of the...
6
by: nembo kid | last post by:
I have these classes: Boss, CommissionWorker, PieceWorker, HourlyWorker all directly derived from Employeer. I create a dynamic object (on heap) for each class:
50
by: Juha Nieminen | last post by:
I asked a long time ago in this group how to make a smart pointer which works with incomplete types. I got this answer (only relevant parts included): ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
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
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.