0

I have a double pointer that I am using to create an array of linked lists. Basically I am trying to take the data from my "cities" that is already in an array, and assign these cities in my "row" portion of this double pointer, so that I can iterate through these rows with a seperate "flight" data file, and if they match, then link all of the data onto that certain row in the double pointer.

My type.h file which contains the stuct for the node, etc:

#ifndef TYPE_H
#define TYPE_H
#include<string>

struct flight
{
    int flightID;
    std::string origin;
    std::string destination;
    int price;
};

typedef flight listItemType;

struct Node
{
    listItemType data;
    Node * next;
};

typedef Node ** nodePtr;
typedef Node * node;
#endif

My flightMap.h file which contains all of my class objects:

#include "type.h"
#include <string>


using namespace std;
const int MAX = 50;
#ifndef flightMap_Class
#define flightMap_Class

class flightMapClass
{
    private:
        int size;
        string* cities;
        nodePtr flights;
        node Head;

    public:
        flightMapClass();
        ~flightMapClass();
        void readCities();
        void readFlights();
};

#endif

and my flightMap.cpp which contains the operations for these objects:

#include "flightMap.h"
#include <string>
#include <iostream>
#include <cstdlib>
#include <fstream>

using namespace std;

flightMapClass::flightMapClass()
{
    size = 0;
    Head = NULL;
}

flightMapClass::~flightMapClass()
{
    node cur = Head;

    while(Head!=NULL)
    {
        cur->next = NULL;
        delete cur;
        Head = Head->next;
        cur = Head;
    }
}

void flightMapClass::readCities()
{
    int index = 0;
    ifstream fin;
    fin.open("cities.dat");
    fin >> size;
    cities = new string[size];
    while(fin.peek()!=EOF)
    {
        fin >> cities[index];
        index ++;
    }
    for(int i = 0; i < index -1; i++)
    {
        cout << cities[i] << endl;
    }
    fin.close();

}

void flightMapClass::readFlights()
{
    cout <<"Reading into Flight Data" << endl;
    flights = new Node * [size];
    for(int i = 0; i < size; i++)
    {
        flights[i]->data.origin = cities[i];
        cout << flights[i]->data.origin << endl;
    }
}

When I try and run the program, here is the output.. (in my main file, I have it running readCities first, and then readFlights, so I have determined that the cities do actually load correctly into the array that I am loading in "readCities", since they do infact output correctly) :::

Albuquerque
Chicago
San-Diego
Nashville
San-Francisco
Miami
Dallas
Washington-DC
St-Louis
New-York-City
Los-Angeles
Boston
Las-Vegas
Orlando
Columbus
Seattle
Atlanta
Memphis
Houston
Austin
Denver
Minneapolis
Tampa
Portland
Kansas-City
Phoenix
Philadelphia
San-Jose
Charlotte
Detroit

reading flights
Reading into Flight data
Segmentation fault

... I have essentially determined that it is coming from these lines of code::

flights[i]->data.origin = cities[i];
cout << flights[i]->data.origin << endl;

How would I assign data into the "rows" portion of my flights so as to not get a segmentation fault? Is this not the correct way to set it up, because by the looks of it to me, its assigning a string to a string? I am confused.

4

1 回答 1

3
flights = new Node * [size];

是不足够的。这只是一个节点指针数组。指针尚未指向已分配的节点。

您还需要分配每个节点。

for(int i = 0; i < size; i++)
{
    flights[i] = new Node;
    ^^^^^^^^^^^^^^^^^^^^^^^^^^
    flights[i]->data.origin = cities[i];
    cout << flights[i]->data.origin << endl;
}
于 2013-04-17T17:55:13.727 回答