Search code examples
omnet++inet

How to solve the error "const inet::MacHeaderBase’ as ‘this’ argument discards qualifiers [-fpermissive]"


I am using the following header structure:

class MacHeaderBase extends FieldsChunk
{
    MacAddress srcAdd;
    MacAddress destAdd;
    MacTypes type; 
    int morebit; 
      
}

To set the morebit, I wrote hdr->setMorebit(1) as follows:

    auto packet = currentTxFrame->dup();
    const auto& hdr = packet->peekAtFront<MacHeaderBase>(); 
    DestAddr = hdr->getDestAddr();
    hdr->setMorebit(1);

However, I am getting error:

passing ‘const inet::MacHeaderBase’ as ‘this’ argument discards qualifiers [-fpermissive] hdr->setMorebit(1);

Can anyone suggest how to resolve this error?


Solution

  • Method peetAtFront() returns an immutable chunk so one cannot change it. Use makeExclusivelyOwnedMutableChunk to convert it to a mutable chunk this way:

    auto packet = currentTxFrame->dup();
    auto& hdr = makeExclusivelyOwnedMutableChunk(packet->peekAtFront<MacHeaderBase>()); 
    DestAddr = hdr->getDestAddr();
    hdr->setMorebit(1);        
    

    EDIT
    peekAtFront() should be used when one want to read a chunk and its fields. The recommended way to modify a chunk of a packet is:

    • use removeAtFront() to obtain a chunk and remove it from a packet
    • modify that chunk
    • insert that chunk to a packet using insertAtFront()