Problem with CMapStringToPtr Lookup function

P

Prasad

HI.

I have written the following code..in VC++(win32 console with MFC
support)

CMapStringToPtr chat;
vector<UserMessage *> v;
/*
UserMessage is a class..
*/
UserMessage *obj_msg=new
UserMessage(fromUser,fromUserName,'n',msg);
void * rValue;
CString toUser="abcd";
v.push_back(obj_msg); // Insering an object into vector v
chat.SetAt(toUser,&v); // Putting the vector 's reference into map
"chat"
chat.Lookup(toUser,rValue);// Getting the vector 's reference from
map "chat"

vector<UserMessage *> * vec=((vector<UserMessage *> *)rValue);
if(!vec->empty())
{
/* do some thing */
}
else
{
printf("Vector is empty");
}

///
It's not giving any compile errors...But..eventhough I put UserMessage
object into vector,
"Vector is empty " is being displayed on console..
(
I think the problem is with map Lookup function and typecasting
statement
vector<UserMessage *> * vec=((vector<UserMessage *> *)rValue);
)

I am just guessing it..
Why is this problem coming ?
plz help me out giving the solution to this problem

Thanx in advance,

Prasad
 
V

Victor Bazarov

Prasad said:
I have written the following code..in VC++(win32 console with MFC
support)

Well, MFC is off-topic here, but we could try to speculate what things
you used do.

Is this code all in one block or not?
CMapStringToPtr chat;
vector<UserMessage *> v;

So, is this a local object in some function?
/*
UserMessage is a class..
*/
UserMessage *obj_msg=new
UserMessage(fromUser,fromUserName,'n',msg);
void * rValue;
CString toUser="abcd";
v.push_back(obj_msg); // Insering an object into vector v
chat.SetAt(toUser,&v); // Putting the vector 's reference into map
"chat"

And here you're storing the pointer to that local object in your map?
chat.Lookup(toUser,rValue);// Getting the vector 's reference from
map "chat"

vector<UserMessage *> * vec=((vector<UserMessage *> *)rValue);
if(!vec->empty())
{
/* do some thing */
}
else
{
printf("Vector is empty");
}

///
It's not giving any compile errors...But..eventhough I put
UserMessage object into vector,
"Vector is empty " is being displayed on console..

You need to take your code and wrap it into an executable piece so
we would be looking at a _complete_ program.
(
I think the problem is with map Lookup function and typecasting
statement
vector<UserMessage *> * vec=((vector<UserMessage *> *)rValue);

I don't see any problem here. You ought to use 'static_cast', of
course, but the C-style case should behave just like it.
)

I am just guessing it..
Why is this problem coming ?

No idea. Most likely, if you're doing storing and looking up in
two different scopes, the local object 'v' goes out of existence
and the stored pointer is invalid.

V
 
P

Prasad

Victor said:
Well, MFC is off-topic here, but we could try to speculate what things
you used do.

Is this code all in one block or not?


So, is this a local object in some function?


And here you're storing the pointer to that local object in your map?


You need to take your code and wrap it into an executable piece so
we would be looking at a _complete_ program.


I don't see any problem here. You ought to use 'static_cast', of
course, but the C-style case should behave just like it.


No idea. Most likely, if you're doing storing and looking up in
two different scopes, the local object 'v' goes out of existence
and the stored pointer is invalid.

V


Thanx for ur reply,

The whole code is in a single block of code..
( "chat" map variable is global ..So ,eventhough inserting a vector
reference into map table is in one block of code and getting the
reference of the vector in another block of code..i don't think the
scope of vector variable matters as i am storing its reference
....anyway ..we can put the whole code in a single block(main)..)
Any pointers now?

regrads
Prasad
 
V

Victor Bazarov

Prasad said:
[...]
The whole code is in a single block of code..
( "chat" map variable is global ..So ,eventhough inserting a vector
reference into map table is in one block of code and getting the
reference of the vector in another block of code..i don't think the
scope of vector variable matters as i am storing its reference
...anyway ..we can put the whole code in a single block(main)..)
Any pointers now?

'microsoft.public.vc.mfc'

V
 
P

Prasad

Victor said:
Prasad said:
[...]
The whole code is in a single block of code..
( "chat" map variable is global ..So ,eventhough inserting a vector
reference into map table is in one block of code and getting the
reference of the vector in another block of code..i don't think the
scope of vector variable matters as i am storing its reference
...anyway ..we can put the whole code in a single block(main)..)
Any pointers now?

'microsoft.public.vc.mfc'

V

thanx,
i already posted in that group,...
 

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,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top