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

放肆的年华 提交于 2021-01-28 07:16:34

问题


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.

  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.



来源:https://stackoverflow.com/questions/45801241/omnet-how-to-access-function-or-variables-in-another-class

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!