OMNET++ 如何访问另一个函数或变量 class

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

我修改了 inet NodeStatus.cc 自定义函数 return 变量值如下:

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

然后,我创建了另一个名为 simpleNodeB.cc 的简单模块,我想从 NodeStatus.cc 中检索 ValueA。我在 simpleNodeB.cc 中尝试了以下代码,但没有成功:

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

我收到的错误消息 -> 错误:')' 标记前的预期主表达式。我不确定我是否使用了正确的方法来调用 getValueA() 函数。请赐教。非常感谢。

你的代码有很多错误。

  1. 方法 getSubmodule 需要 a name of module,而不是 class 的名称。查看您的 NED 文件并检查此模块的实际名称。
  2. getSubmodule returns 指向 cModule 对象的指针。它必须手动转换为另一个 class.

假设 NED 中的一个 NodeStatus 模块被命名为 fooStatus,正确的代码应该如下所示:

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

参考:OMNeT++ Manual.