Search code examples
omnet++inet

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


I am trying to create a new UDP application with which I can control the process of generating packets and the process of sending packets i.e. I have separate parameters for packet generation and send interval.

When I am running the code, getting the following errors:

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); ^~~~~~~

The chunk of code is given below:

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++;
}

Would anyone suggest if the code is correct and how to resolve the errors?

Thank you


Solution

  • The declaration of insert() is following:

    virtual void insert(cObject *obj);
    

    Therefore one cannot pass other object that cObject or derived class. You may use an instance of Packet because it inherits from cObject. So the code may look like:

    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++;
            }
        }
    }