如何修复调用错误和“ ”未在此范围错误中声明的无匹配函数

How to fix no matching function for call error and ' ' was not declared in this scope errors

我正在尝试创建一个新的 UDP 应用程序,通过它我可以控制生成数据包的过程和发送数据包的过程,即我有单独的数据包生成和发送间隔参数。

我在运行代码的时候,出现如下错误:

inet/applications/udpapp/UdpOwnApp.cc:134:29: error: no matching function for call to ‘omnetpp::cQueue::insert(const inet::IntrusivePtrinet::ApplicationPacket&)’ queue.insert(payload); ^ inet/applications/udpapp/UdpOwnApp.cc:157:15: error: ‘payload’ was not declared in this scope queue.pop(payload); ^~~~~~~

代码块如下:

void UdpOwnApp::generatePacket()
{
        const auto& payload = makeShared<ApplicationPacket>();
        payload->setChunkLength(B(par("messageLength")));
        payload->setSequenceNumber(numGenerate);
        payload->addTag<CreationTimeTag>()->setCreationTime(simTime());
        queue.insert(payload);
        simtime_t d = simTime() + par("generateInterval");
        if (stopTime < SIMTIME_ZERO || d < stopTime) {
            selfMsg->setKind(GENERATE);
            scheduleAt(d, selfMsg);
        }
        else {
            selfMsg->setKind(STOP);
            scheduleAt(stopTime, selfMsg);
        }
}
void UdpOwnApp::sendPacket()
{
    std::ostringstream str;
    str << packetName << "-" << numSent;
    Packet *packet = new Packet(str.str().c_str());
    if(dontFragment)
        packet->addTag<FragmentationReq>()->setDontFragment(true);
    queue.pop(payload);
    packet->insertAtBack(payload); 
    L3Address destAddr = chooseDestAddr();
    emit(packetSentSignal, packet);
    socket.sendTo(packet, destAddr, destPort);
    numSent++;
}

有人可以建议代码是否正确以及如何解决错误吗?

谢谢

insert()的声明如下:

virtual void insert(cObject *obj);

因此不能传递 cObject 或派生 class 的其他对象。您可以使用 Packet 的实例,因为它继承自 cObject。所以代码可能看起来像:

void UdpOwnApp::generatePacket() {
    const auto& payload = makeShared<ApplicationPacket>();
    payload->setChunkLength(B(par("messageLength")));
    payload->setSequenceNumber(numGenerate);
    payload->addTag<CreationTimeTag>()->setCreationTime(simTime());

    std::ostringstream str;
    str << packetName << "-" << numSent;
    Packet *packet = new Packet(str.str().c_str());
    packet->insertAtBack(payload);
    queue.insert(packet);
    simtime_t d = simTime() + par("generateInterval");
    if (stopTime < SIMTIME_ZERO || d < stopTime) {
       selfMsg->setKind(GENERATE);
       scheduleAt(d, selfMsg);
    } else {
       selfMsg->setKind(STOP);
       scheduleAt(stopTime, selfMsg);
    }
}

void UdpOwnApp::sendPacket() {
    if (queue.isEmpty()) {
        // no packets in queue
    } else {
        cObject * obj = queue.pop();
        Packet * packet = dynamic_cast<Packet*>(obj);
        if (packet) {
            if (dontFragment)
                packet->addTag<FragmentationReq>()->setDontFragment(true);
            L3Address destAddr = chooseDestAddr();
            emit(packetSentSignal, packet);
            socket.sendTo(packet, destAddr, destPort);
            numSent++;
        }
    }
}