output a list<> within a struct

krazeegook
krazeegook used Ask the Experts™
on
I have a list within a struct like so:
typedef struct{
  int parent;     //node number
  int tDist;  

  typedef struct{
    int destNode;    //destination node
    int cDist;    
  }listNode;

  listNode lN;

  list< listNode > aList;

} heapNode;

I have initiated them like so:

  /***create adjacency list***/
  for(int j=0;j<N[0];j++){
    heapNode hN;
    hN.parent = 0;     //parent = null
    hN.tDist = 999999; //infinity value
    adjList.push_back(hN);
  }
  //sets up the list within each node
  for(int j=2;j<N.size();j=j+3){
    //N[j],N[j+1],N[j+2] are just bunch of ints
    adjList[N[j]].lN.destNode = N[j+1];
    adjList[N[j]].lN.cDist = N[j+2];
    adjList[N[j]].l.push_back(adjList[j].lN);
  }

and I'm trying to print out integers in struct elements of "aList". I am failing miserably and gets some wierd values as output.  How should I go about this?
(I would also need the int values within the listNode structs for assignment or comparison later)

THanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
krazeegook,
The code seems a little mixed up and parts are missing.

I suggest that you arrenge it a little.
Start with the h file bye separating the struct in a way that the inner struct is defined befor the outer one.

Then in the cpp file verify that all your variables are of the correct type you want. Here some variable types are not clear.

After that test the program again.

If it still fails please copy the contents of the new h and cpp files and post a corrected version of your question.

Commented:
sorry for the typing mistakes

Commented:
i recommend using the STL's for_each<>().

create a functional object that prints a single node, and run for_each with it (easier to debug)

Author

Commented:
Thanks Mafalda,
I rearranged so that inner struct is declared before outter struct and it works fine =)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial