OMNET++ 如何访问另一个类中的函数或变量 [英] OMNET++ how to access function or variables in another class
问题描述
我已经使用自定义函数修改了 inet NodeStatus.cc,该函数返回变量值如下:
I have modified inet NodeStatus.cc with a customized function that return the variable value as follows:
int NodeStatus::getValueA()
{
return ValueA;
}
然后,我创建了另一个名为 simpleNodeB.cc 的简单模块,我想从 NodeStatus.cc 中检索 ValueA.我在 simpleNodeB.cc 中尝试了以下代码,但没有奏效:
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");
我收到的错误消息 -> 错误:在 ')' 标记之前预期预期的主表达式.我不确定我是否使用了正确的方法来调用 getValueA() 函数.请赐教.非常感谢.
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.
推荐答案
您的代码中有很多错误.
There are many errors in your code.
getSubmodule
方法需要 模块名称,不是类名.查看您的NED
文件并检查此模块的实际名称.getSubmodule
返回一个指向cModule
对象的指针.必须手动将其转换为另一个类.
- 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.
假设您的 NED
中的 NodeStatus
模块名为 fooStatus
,正确的代码应如下所示:
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");
参考:OMNeT++ 手册.
这篇关于OMNET++ 如何访问另一个类中的函数或变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!