badman
badman

Reputation: 61

OMNET++ how to access function or variables in another class

I have modified inet NodeStatus.cc with a customized function that return the variable value as follows:

 int NodeStatus::getValueA()
 {
return ValueA;
 }

Then, I created another simple module called simpleNodeB.cc and I wanted to retrieve ValueA from NodeStatus.cc. I tried the following code in simpleNodeB.cc but didn't work:

 if(getParentModule()->getSubModule(NodeStatus).getValueA()==test1)
                        bubble("Value is the same");

The error message I got -> error: expected expected primary-expression before ')' token. I'm not sure if I used the correct way to call getValueA() function. Please enlighten me. thanks a lot.

Upvotes: 1

Views: 1260

Answers (1)

Jerzy D.
Jerzy D.

Reputation: 7002

There are many errors in your code.

  1. The method getSubmodule requires a name of module, not a name of class. Look at your NED file and check the actual name of this module.
  2. getSubmodule returns a pointer to the cModule object. It has to be manually cast into another class.

Assuming that an NodeStatus module in your NED is named fooStatus the correct code should look like:

cModule *mod = getParentModule()->getSubmodule("fooStatus");
NodeStatus *status = check_and_cast<NodeStatus*>(mod);
if(status->getValueA() == test1) 
    bubble("Value is the same");

Reference: OMNeT++ Manual.

Upvotes: 1

Related Questions