By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,657 Members | 994 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,657 IT Pros & Developers. It's quick & easy.

Problem with CMapStringToPtr Lookup function

P: n/a
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

Jun 19 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Prasad wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 19 '06 #2

P: n/a

Victor Bazarov wrote:
Prasad wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

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

Jun 19 '06 #3

P: n/a
Prasad wrote:
[...]
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 19 '06 #4

P: n/a

Victor Bazarov wrote:
Prasad wrote:
[...]
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


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

Jun 19 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.