问题
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.
回答1:
There are many errors in your code.
- The method
getSubmodule
requires a name of module, not a name of class. Look at yourNED
file and check the actual name of this module. getSubmodule
returns a pointer to thecModule
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.
来源:https://stackoverflow.com/questions/45801241/omnet-how-to-access-function-or-variables-in-another-class