Omnet++ OMNET++;如何访问另一个类中的函数或变量

Omnet++ OMNET++;如何访问另一个类中的函数或变量,omnet++,inet,Omnet++,Inet,我使用一个自定义函数修改了inet NodeStatus.cc,该函数返回变量值,如下所示: int NodeStatus::getValueA() { return ValueA; } 然后,我创建了另一个名为simpleNodeB.cc的简单模块,我想从NodeStatus.cc检索ValueA。我在simpleNodeB.cc中尝试了以下代码,但没有成功: if(getParentModule()->getSubModule(NodeStatus).getValueA()=

我使用一个自定义函数修改了inet NodeStatus.cc,该函数返回变量值,如下所示:

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

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

我收到的错误消息->错误:在“')标记之前应该有主表达式。我不确定调用getValueA()函数的方法是否正确。请开导我。非常感谢。

您的代码中有许多错误

  • 方法
    getSubmodule
    需要,而不是类的名称。查看您的
    NED
    文件并检查此模块的实际名称
  • getSubmodule
    返回指向
    cModule
    对象的指针。它必须手动转换到另一个类中
  • 假设
    NED
    中的
    NodeStatus
    模块名为
    fooStatus
    正确的代码如下所示:

    cModule *mod = getParentModule()->getSubmodule("fooStatus");
    NodeStatus *status = check_and_cast<NodeStatus*>(mod);
    if(status->getValueA() == test1) 
        bubble("Value is the same");
    
    cModule*mod=getParentModule()->getSubmodule(“fooStatus”);
    NodeStatus*状态=检查和铸造(mod);
    如果(状态->getValueA()==test1)
    泡沫(“价值相同”);
    
    参考文献: