struct containing vector?

Discussion in 'C++' started by cerr, Feb 1, 2010.

  1. cerr

    cerr Guest

    Hi There,

    I have a little issue going here:
    I have a xml configuration file that looks kinda like this:
    <PID>
    <DESCRIPTION>Steveston Hwy</DESCRIPTION>
    <BCAST>192.168.101.255</BCAST>
    <DESTINATION>
    <DESTNO>1531</DESTNO>
    <DESTNO>301</DESTNO>
    <DESTNO>12555</DESTNO>
    </DESTINATION>
    </PID>
    Now there could be various <PID> tags as well as each <PID> would
    likely have multiple <DESTNO> tags.
    How do I store this best in my app?
    I thoughtr I'd declare a struct like this:
    struct structPID
    {
    string strBcast;
    string strDescription;
    vector<int> Dest;
    };
    and just extend the array when there's more PIDs in that file.
    But when I read out the file i'm using my xml parser and i thought i'd
    do something like this:
    //i_PIDlist is the pointer to a variable decalred private in class
    PIDClient.
    bool PIDClient::readPIDConfig(string Configfile, vector<structPID>
    *i_PIDlist)
    {
    RSXMLParser xmlPIDData(Configfile,
    RSXMLParser::FILE);

    if (!xmlPIDData.IsDataGood()) {
    OUTPUT(std::cerr << "Document " << conf->
    intersectionDataFile << " is not parsed
    successfully.\n";
    )
    return false;
    }

    string strPID = xmlPIDData.GetNodeData("PID"); //PID
    while (strPID.size() > 0) {
    RSXMLParser xmlPID(strPID); //PID details
    structPID tmpPID;

    tmpPID.strBcast=xmlPID.GetNodeData("BCAST");
    tmpPID.strDescription=xmlPID.GetNodeData("DESCRIPTION");

    string strDest = xmlPIDData.GetNodeData("DESTINATION");
    while(strDest.size() > 0) {
    RSXMLParser xmlPIDDest(strDest);
    xmlPIDDest.GetNodeData("DESTNO");
    tmpPID.Dest->push_back();
    }
    i_PIDlist->push_back(tmpPID);
    //delete tmpPID;
    //tmpPID=NULL;

    strPID = xmlPIDData.GetNext();
    }

    return true;
    }
    But there's two problems i'm seeing just right out of the bat:
    If the outer while loop goes twice, how would tmpPID be re-declared?
    I thought about declaring it on the heap and then calling delete in
    the end of the funtion before it loops around but even then,
    I would do a i_PIDlist->push_back(tmpPID) and it woudl push on a
    pionter that gets ereased soon after...

    Thanks for a little help and guidance here.

    Ron
     
    cerr, Feb 1, 2010
    #1
    1. Advertising

  2. cerr

    cerr Guest

    On Feb 1, 8:45 am, cerr <> wrote:
    > Hi There,
    >
    > I have a little issue going here:
    > I have a xml configuration file that looks kinda like this:
    > <PID>
    >   <DESCRIPTION>Steveston Hwy</DESCRIPTION>
    >   <BCAST>192.168.101.255</BCAST>
    >   <DESTINATION>
    >     <DESTNO>1531</DESTNO>
    >     <DESTNO>301</DESTNO>
    >     <DESTNO>12555</DESTNO>
    >   </DESTINATION>
    > </PID>
    > Now there could be various <PID> tags as well as each <PID> would
    > likely have multiple <DESTNO> tags.
    > How do I store this best in my app?
    > I thoughtr I'd declare a struct like this:
    > struct structPID
    > {
    >         string strBcast;
    >         string strDescription;
    >         vector<int> Dest;};
    >
    > and just extend the array when there's more PIDs in that file.
    > But when I read out the file i'm using my xml parser and i thought i'd
    > do something like this:
    > //i_PIDlist is the pointer to a variable decalred private in class
    > PIDClient.
    > bool PIDClient::readPIDConfig(string Configfile, vector<structPID>
    > *i_PIDlist)
    > {
    >     RSXMLParser xmlPIDData(Configfile,
    >                              RSXMLParser::FILE);
    >
    >     if (!xmlPIDData.IsDataGood()) {
    >         OUTPUT(std::cerr << "Document " << conf->
    >                          intersectionDataFile << " is not parsed
    > successfully.\n";
    >               )
    >         return false;
    >     }
    >
    >     string strPID = xmlPIDData.GetNodeData("PID");      //PID
    >     while (strPID.size() > 0) {
    >         RSXMLParser xmlPID(strPID);                     //PID details
    >         structPID tmpPID;
    >
    >         tmpPID.strBcast=xmlPID.GetNodeData("BCAST");
    >         tmpPID.strDescription=xmlPID.GetNodeData("DESCRIPTION");
    >
    >         string strDest = xmlPIDData.GetNodeData("DESTINATION");
    >         while(strDest.size() > 0) {
    >           RSXMLParser xmlPIDDest(strDest);
    >           xmlPIDDest.GetNodeData("DESTNO");
    >           tmpPID.Dest->push_back();
    >         }
    >         i_PIDlist->push_back(tmpPID);
    >         //delete tmpPID;
    >         //tmpPID=NULL;
    >
    >         strPID = xmlPIDData.GetNext();
    >     }
    >
    >     return true;}
    >
    > But there's two problems i'm seeing just right out of the bat:
    > If the outer while loop goes twice, how would tmpPID be re-declared?
    > I thought about declaring it on the heap and then calling delete in
    > the end of the funtion before it loops around but even then,
    > I would do a i_PIDlist->push_back(tmpPID) and it woudl push on a
    > pionter that gets ereased soon after...
    >
    > Thanks for a little help and guidance here.
    >
    > Ron


    Ah,

    I think I've figured out how I can do it:
    How about this?

    while (strPID.size() > 0) {
    RSXMLParser xmlPID(strPID); //PID details
    structPID *tmpPID = new structPID;

    tmpPID->strBcast=xmlPID.GetNodeData("BCAST");
    tmpPID->strDescription=xmlPID.GetNodeData("DESCRIPTION");

    string strDest = xmlPIDData.GetNodeData("DESTINATION");
    while(strDest.size() > 0) {
    RSXMLParser xmlPIDDest(strDest);
    tmpPID->Dest.push_back(atoi(xmlPIDDest.GetNodeData("DESTNO").c_str
    ()));
    }
    i_PIDlist->push_back((*tmpPID));
    delete tmpPID;
    tmpPID=NULL;

    strPID = xmlPIDData.GetNext();
    }
     
    cerr, Feb 1, 2010
    #2
    1. Advertising

  3. On Feb 1, 5:45 pm, cerr <> wrote:
    > Hi There,
    >
    > I have a little issue going here:
    > I have a xml configuration file that looks kinda like this:

    <snip>
    >     string strPID = xmlPIDData.GetNodeData("PID");      //PID
    >     while (strPID.size() > 0) {
    >         RSXMLParser xmlPID(strPID);                     //PID details
    >         structPID tmpPID;

    <snip>
    >         i_PIDlist->push_back(tmpPID);
    >         //delete tmpPID;
    >         //tmpPID=NULL;
    >
    >         strPID = xmlPIDData.GetNext();
    >     }
    >
    >     return true;}
    >
    > But there's two problems i'm seeing just right out of the bat:
    > If the outer while loop goes twice, how would tmpPID be re-declared?


    These is no need to do anything special.
    If the outer loop iterates multiple times, the brace-enclosed body of
    the loop is entered and exited multiple times. At each entry, the
    local variables are created anew by the compiler, and at each exit of
    the loop body, they are destroyed.

    So, if your loop iterates twice, then you will have two distinct
    objects named tmpPID, with one being created after the other.

    > I thought about declaring it on the heap and then calling delete in
    > the end of the funtion before it loops around but even then,
    > I would do a i_PIDlist->push_back(tmpPID) and it woudl push on a
    > pionter that gets ereased soon after...
    >
    > Thanks for a little help and guidance here.
    >
    > Ron


    Bart v Ingen Schenau
     
    Bart van Ingen Schenau, Feb 3, 2010
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. pmatos
    Replies:
    6
    Views:
    23,817
  2. Chris Fogelklou
    Replies:
    36
    Views:
    1,391
    Chris Fogelklou
    Apr 20, 2004
  3. Replies:
    8
    Views:
    1,930
    Csaba
    Feb 18, 2006
  4. Javier
    Replies:
    2
    Views:
    567
    James Kanze
    Sep 4, 2007
  5. Rushikesh Joshi
    Replies:
    0
    Views:
    364
    Rushikesh Joshi
    Jul 10, 2004
Loading...

Share This Page