[Omnetpp-l] cannot cast cMessage to cPacket

535 views
Skip to first unread message

Vitali Anselm

unread,
Apr 7, 2009, 4:42:04 AM4/7/09
to omne...@omnetpp.org
Hello,

I use some functions like getByteLength() in my handleMessage(cMessage *msg) function, so I decided to cast msg to cPacket to be able to use these functions. For this I wrote the following code:

cPacket *tempPtr = check_and_cast<cPacket *>(msg);

and then I use tempPtr at places, where the cPacket functions are called.

When I compile all, there is no errors, but when I want to run TKenv there is
the following error:

Error in module(.....):
check_and_cast():cannot cast (cMessage*).......[0].per_second_timer to type
"P7cPacket".

per_second_timer is declared as cMessage:

per_second_timer = new cMessage("per_second_timer");
scheduleAt(simTime(), per_second_timer);

Can you help me?

regards,

Vitali
____________________________________________________________________
Psssst! Schon vom neuen WEB.DE MultiMessenger gehört?
Der kann`s mit allen: http://www.produkte.web.de/messenger/?did=3123

_______________________________________________
Omnetpp-l mailing list
Omne...@omnetpp.org
http://lists.omnetpp.org/mailman/listinfo/omnetpp-l

Ahmed

unread,
Apr 7, 2009, 4:35:30 PM4/7/09
to omne...@omnetpp.org
Hello,

try to use cPacket * PK(cMessage  *) to cast cMessage to cPacket.

regards,
--
Ahmed

Alfonso Ariza Quintana

unread,
Apr 9, 2009, 10:33:13 AM4/9/09
to omne...@omnetpp.org

it's inherit problem

cPacket is a descendant of cMessage

if you create a

cPacket *pkt = new cPacket();
cMessage *msg = pkt; // Valid   cPacket is a descendant of cMessage
cPacket *pkt2 = dynamic_cast<cPacket*>(msg);


 pkt2 <> null because msg containt a cPacket


 but if you do

cMessage *msg = new cMessage();
cPacket *pkt2 = dynamic_cast<cPacket*>(msg);

pkt2 = NULL because msg not contain a cPacket, contain a cMessage


> Date: Tue, 7 Apr 2009 10:42:04 +0200
> From: Vitali...@web.de
> To: omne...@omnetpp.org
> Subject: [Omnetpp-l] cannot cast cMessage to cPacket

Haz búsquedas desde Live Search y ayuda a 22 niños de un orfanato en Nepal ¡Colabora!

ktn varma

unread,
Feb 19, 2013, 12:22:52 AM2/19/13
to omn...@googlegroups.com, omne...@omnetpp.org
So, can someone help me with a solution to this problem...

Rudolf Hornig

unread,
Feb 19, 2013, 5:32:03 AM2/19/13
to omn...@googlegroups.com, omne...@omnetpp.org
This is a 4 year old topic. If you are interested in the solution:

an object with dynamic type cMessage cannot be cast to cPacket. That's why the code has failed on check_and_cast
Reply all
Reply to author
Forward
0 new messages