我正在尝试在子类 PassiveQueueBase 中设置一个参数,并在基类 IPassiveQueue 的另一个子类 PriorityScheduler 中调用它。
PriorityScheduler 继承自 SchedulerBase,它有一个指针(inputQueue)指向谁是 IpassiveQueue 类型并将调用 PacketEnqueued() 函数给 PriorityScheduler。这显示在这段代码中
class INET_API SchedulerBase : public cSimpleModule, public IPassiveQueue, public IPassiveQueueListener
{
public:
virtual void packetEnqueued(IPassiveQueue *inputQueue) override;
};
注意:IpassiveQueue.h 没有.cc 文件,它是许多其他类的接口(interface)文件。
我尝试做的如下所示,
class INET_API IPassiveQueue
{
public:
simtime_t sendWindowStart;
simtime_t sendWindowEnd;
virtual void setWindows() {};
};
class INET_API PassiveQueueBase : public cSimpleModule, public IPassiveQueue
{ protected:
void setWindows(simtime_t SWS,simtime_t SWE);
};
void PassiveQueueBase :: setWindows(simtime_t SWS,simtime_t SWE)
{ SWS = 3; SWE = 4;
sendWindowStart = SWS;
sendWindowEnd = SWE;
};
bool PriorityScheduler::schedulePacket()
{
IPassiveQueue *pqueue;
pqueue->setWindows();
sendWindowStart = inputQueue->sendWindowStart;
sendWindowEnd = inputQueue->sendWindowEnd;
}
运行模拟时出现错误,如下所示
Simulation terminated with exit code: 139 Working directory: /home/usrname/omnetpp-5.0/samples/inet/examples/mysimulation Command line: opp_run -r 0 -n ..:../../src:../../tutorials -l ../../src/INET --debug-on-errors=false omnetpp.ini
另外我在这一行上有感叹号
pqueue->setWindows();
附上这张便条
‘Pqueue’ may be used uninitialized in this function [-Wmaybe-uninitialized]
最佳答案
你还没有分配pqueue
,你正在使用它。
IPassiveQueue *pqueue;
这个仍然指向内存中的未知位置。
要解决此问题,您可以使用 new
IPassiveQueue *pqueue = new IPassiveQueue;
并确保使用 delete
delete pqueue;
你也可以使用智能指针分配它,这样会更安全
std::unique_ptr<IPassiveQueue> pqueue(new IPassiveQueue);
关于c++ - Omnet++ 错误在子类中设置参数并在另一个子类中调用它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40721906/