C++ 使用INET扩展模块未正确执行重写代码

C++ 使用INET扩展模块未正确执行重写代码,c++,module,omnet++,inet,C++,Module,Omnet++,Inet,我必须在OMNeT++5.6.1(Ubuntu 18.04.4)中从INET4扩展UdpBasicApp模块,并执行两个重写方法(initialize和handleMessageWhenUp)。 这似乎是一项简单的任务,在我构建项目(暂时存储在inet4的“examples”文件夹中)时看起来一切都很好,没有出现错误,但在开始模拟之后,代码没有成功重写。而是执行原始代码(来自UDPBASICAP的代码)。 事实上,在编辑器中检查代码后,方法的名称并不像应该的那样粗体,关键字的颜色也不像通常应该的

我必须在OMNeT++5.6.1(Ubuntu 18.04.4)中从INET4扩展UdpBasicApp模块,并执行两个重写方法(initialize和handleMessageWhenUp)。 这似乎是一项简单的任务,在我构建项目(暂时存储在inet4的“examples”文件夹中)时看起来一切都很好,没有出现错误,但在开始模拟之后,代码没有成功重写。而是执行原始代码(来自UDPBASICAP的代码)。 事实上,在编辑器中检查代码后,方法的名称并不像应该的那样粗体,关键字的颜色也不像通常应该的那样。 我的C++技能不高(我的OMNET也不高),所以我不确定到底发生了什么。 扩展的简单模块称为“PriorityApp”(我只添加了一个整数字段)

这是.ned代码(不确定是否需要)

这是.h代码

#ifndef __INET4_PRIORITYAPP_H_
#define __INET4_PRIORITYAPP_H_

#include "inet/applications/udpapp/UdpBasicApp.h"

namespace inet {

class PriorityApp : public UdpBasicApp
{
  protected:
    int priority;

    virtual void initialize(int stage) override;
    virtual void handleMessageWhenUp(cMessage *msg) override;
};

} // namespace inet

#endif  // ifndef __INET_PRIORITYAPP_H
这是.cc代码

#include "PriorityApp.h"

namespace inet {

Define_Module(PriorityApp);

void PriorityApp::initialize(int stage)
{
    msg = new cMessage("priority");
    EV << "Example Message" << endl;
}

void PriorityApp::handleMessageWhenUp(cMessage *msg)
{

}

} //namespace inet

这里有两个独立的故障:

不应将C++代码添加到IET的示例文件夹中。该文件夹未指定为源文件夹,因此生成系统不会拾取、生成并链接到可执行文件,这意味着您的代码不在模型中。这就是为什么你没有得到你的新行为的原因之一

2.)即使您将源代码与UdpBasicApp一起放入
src
文件夹,并且构建系统会将其正确链接,您仍然必须正确设置NED零件。具体来说,您必须指定@类属性来告诉ND,您打算使用的C++类指定行为:<代码> @类(PrimyIdApp)< /Cord>。否则您的PrimyIdApp模块将继承UdpBasicApp的@类属性值,并使用CDPasiCpp C++类。p>
import inet.applications.udpapp.UdpBasicApp;

// The module extends UdpBasicApp adding a priority parameter
simple PriorityApp extends UdpBasicApp
{
    @class(PriorityApp)
    parameters:
        int priority = default(0);  // priority 0 = higher priority
}

如果这是C++,那么构造函数在哪里?为什么要初始化代码?这是正在使用的库的工件吗?没有构造函数(我现在忘了)。在我以前的尝试中,我试图定义它,但问题仍然存在,没有发生什么不同。我必须假设这不是问题所在。还有,我不知道你说的“神器”是什么意思,对不起。“Initialize”应该是启动模拟时调用的第一个方法。您能显示您的
omnetpp.ini
的内容吗?当然!我刚刚用.ini文件编辑了这个问题我只是想说我刚刚尝试添加了一个构造函数:没有发生任何事情。谢谢!我已将我的项目移动到工作组目录中的一个独立文件夹中,并在.ned文件中添加了标记,以解决此问题。@Rudi显然,您无法将参数添加到简单模块中。您应该删除@class()之后的前两行。另外,您知道如何在omnet中实时获取统计信息吗?
[General]

[Config SwitchedNet1G]
description = "1Gbps Switched Network"
network = SwitchedNet

**.dataRate = 1Gbps                                         # 1 Gbps channel datarate

# Priority stuff
**.host[0].app[0].priority = 1

# Dst Params
**.sink.app[*].typename = "UdpSink"                         # Destination host (it receives messages)
**.sink.app[*].localPort = 2500

# Src Params
**.host[0].app[0].packetName = "Host0-Data"
**.host[*].app[0].typename = "PriorityApp"                  # Source host (they send messages)
**.host[*].app[0].destPort = 2500
**.host[*].app[0].destAddresses = "sink"
**.host[0].app[0].sendInterval = 3s
**.host[*].app[0].startTime = uniform (0s, 3s)

# EthInterface Setup
**.eth[*].qEncap.typename = "Ieee8021qEncap"                # 8021Q Encapsulation
**.etherSwitch.numEthInterfaces = 2                         # Switch ethInterfaces total number (2)
**.numEthInterfaces = 1

# Global Params
**.messageLength = 100B
**.numHosts = 1
**.numApps = 1                                  # every host has 1 app (UdpBasicApp or UdpSink)
import inet.applications.udpapp.UdpBasicApp;

// The module extends UdpBasicApp adding a priority parameter
simple PriorityApp extends UdpBasicApp
{
    @class(PriorityApp)
    parameters:
        int priority = default(0);  // priority 0 = higher priority
}