Khanh Lê
Khanh Lê

Reputation: 31

check_and_cast() error in Omnet++

I'm simulating network in Omnet++, with INET framework. I want to get the position (coordination x & y) of node. So I do this code:

    cModule *host = getContainingNode(this);
    IMobility *mobility = check_and_cast<IMobility *>(host->getSubmodule("mobility"));
    ... = mobility -> getCurrentPosition();

But, when I ran the simulation, I got this error

check_and_cast(): cannot cast nullptr to type 'inet::IMobility *'

Can you explain to me this error? As I see, if the simulator notify that, so host->getSubmodule("mobility") is nullptr?

By the way, I have define mobilityType in the NED file and include IMobility.h

Upvotes: 1

Views: 2524

Answers (1)

Rudi
Rudi

Reputation: 6681

The NED code you shared shows that you have a simple module that implements the IMobility interface, while the C++ code looks like something that is inside a simple module which is INSIDE a compound module that represent the network host and this compound module also contains a separate simple module called "mobility" that implements the IMobility interface. (this is how code is implemented in INET).

You should either:

  • arrange your code so you have a compound module as a networkNode with several simple modules inside it (one of them is the mobility module).
  • if you insist having a simple module which already implements the IMobility interface (in C++) then you already have access to the position on that object using the getCurrentPosition method.

I assume (and recommend) that you co on the first route, so you should reaarange your NED file.

Upvotes: 2

Related Questions