如何正确打印列表的const char * struct成员?

时间:2019-06-13 09:22:02

标签: c++ omnet++ inet

我试图在OMNeT ++中显示列表的结构成员,除非成员的类型为const char *,否则所有成员均正确显示。我很困惑,因为在显示列表中的三个push_back之后。最后推送的元素的所有成员都可以正确显示,即使类型为const char *。但是对于第一个被推入的两个元素,类型为cont char *的成员不显示任何内容,垃圾内容或“ DETAIL(Ipv4)Drones.host [3] .ipv4.ip”。

Ipv4Address srcAddress = recMtlsd->getSrcAddress();
Ipv4Address dstAddress = recMtlsd->getDstAddress();
const char* position = recMtlsd->getPosition();
simtime_t time = recMtlsd->getTime();
int srcID = recMtlsd->getId();
EV_DEBUG << "Source : " << srcAddress << endl; 
EV_DEBUG << "Destination : " << dstAddress << endl;
EV_DEBUG << "Position : " << position << endl;
EV_DEBUG << "Time : " << time << endl;
EV_DEBUG << "Source ID: " << srcID << endl;
// All precedent displays are working correctly

/*typedef struct Mtlsd{
Ipv4Address originatorAddr, destinationAddr;
const char *position;
int originatorId;
simtime_t time;
}MTLSD;*/

MTLSD recitem;
recitem.originatorAddr = srcAddress;
recitem.originatorId = srcID;
recitem.destinationAddr = dstAddress;
recitem.position = position;
recitem.time = time;
EV_DEBUG << "Source : " << recitem.originatorAddr << endl;
EV_DEBUG << "Destination : " << recitem.dstinationAddr << endl;
EV_DEBUG << "Position : " << recitem.position << endl;
EV_DEBUG << "Time : " << recitem.time << endl;
EV_DEBUG << "Source ID: " << recitem.srcID << endl;
// All precedent displays are working correctly
/*typedef struct Mtlsd_data{
list<MTLSD> q;
int ID;
}MTLSD_DATA;*/
list<MTLSD_DATA> mtlsd_file;

auto node = find_if(mtlsd_file.begin(), mtlsd_file.end(), [=] (MTLSD_DATA const& i){return (i.ID == srcID);});
        bool found = (node != mtlsd_file.end());
        if (!found)
        {
            MTLSD_DATA recdata;
            recdata.ID = srcID;
            recdata.q.push_back(recitem);
            mtlsd_file.push_back(recdata);
            EV_DEBUG << "For node " << srcID ;
            for(auto claim=mtlsd_file.back().q.begin(); claim!=mtlsd_file.back().q.end();++claim)
            {
                EV_DEBUG << "(" << string(claim->position) << ", " << claim->time << ");" << endl;
            }
            // The precedent display works correctly
        }
        else
        {
            EV_DEBUG << "I already have data about the node " << node->ID << endl;
                if (node->q.size() == 3)
                {
                    EV_DEBUG << "I already have three time-location claim in the queue" << endl;
                    EV_DEBUG << "Here they are: ";
                    EV_DEBUG << "For node " << (*node).ID << endl;
                    for(auto fileclaim=(*node).q.begin(); fileclaim!=(*node).q.end();++fileclaim)
                        EV_DEBUG << "(" << string((*fileclaim).position) << ", " << (*fileclaim).time << ");" << endl;
                    EV_DEBUG << "I will delete the old one (" << node->q.front().position << ", " << node->q.front().time << ")" << endl;
                    node->q.pop_front();
                }
                node->q.push_back(recitem);
                EV_DEBUG << "I have pushed this new one : (" << string(node->q.back().position) << ", " << node->q.back().time << ")" << endl;
        }
            EV_DEBUG << "Here they are all time-location claims in the queue : ";
            for(auto fileclaims=node->q.begin(); fileclaims!=node->q.end();++fileclaims)
            {
                EV_DEBUG << "(" << string(fileclaims->position) << ", " << fileclaims->time << ");" << endl;
            }
            // The last element is displayed correctly, but those before not.
            .
            .
            .

0 个答案:

没有答案