dotnabox 0 Newbie Poster

Hey everyone. So I'm stuck trying to insert a class object into my List. I'll try to post what I think is code relevant to my question, but can post more if needed. Basically, I want to have an adjacency list that holds the adjNodes, which contain the weight and vertices connected to each city/node. As it stands, the below code will compile, but will segfault every time it tries to insert into my List.


tripper.h:

class adjNode
{
  public:
    int vertex;
    int weight;
    adjNode(int v, int w) : vertex(v), weight(w) { }
    adjNode() { }
};

tripper.cpp:

Tripper::Tripper(Road *roads, int numRoads, int size)
{
  List <adjNode> adjList;

  for (int i=0; i<numRoads; i++) //Doesn't work with either line
  {
    adjList[roads[i].city1].push_back(adjNode(roads[i].city2, roads[i].distance));
    //adjList[0].push_back(adjNode(2, 15)); //Really it's nothing but 3 integers involved
  }
  for (int i=0; i<9; i++)
  {
    for (List<adjNode>::iterator itr = adjList[i].begin(); itr != adjList[i].end(); itr++)
    {
      cout << "There is an edge going from " << i << " to " << (*itr).vertex;
      cout << " with a weight of " << (*itr).weight << endl;
    }
  }
} // Tripper()

List.h:
push_back():

void push_back( const Object & x )
      { insert( end( ), x ); }

insert():

iterator insert( iterator itr, const Object & x )
    {
        Node *p = itr.current;
        theSize++;
        return iterator( p->prev = p->prev->next = new Node( x, p->prev, p ) );
    }

Thanks again for any direction.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.