C++ 线程1:SIGABRT错误C++;

C++ 线程1:SIGABRT错误C++;,c++,function,class,linked-list,C++,Function,Class,Linked List,在我的程序中使用xcode时,我有一个复杂的错误。这个程序正在C++中进行链表。该程序提示用户在链接列表中输入要搜索的城市,如果找到,则显示用户输入的城市、州和年份。(从文本文件中读取了城市、州和年份,但未在下面的代码中显示) 每当我在程序中输入“QUIT”并提示我进入一个城市时,它就会给我线程1错误,我不知道如何修复它 //HEADER FILE class Data { private: string state; int year; string city; pu

在我的程序中使用xcode时,我有一个复杂的错误。这个程序正在C++中进行链表。该程序提示用户在链接列表中输入要搜索的城市,如果找到,则显示用户输入的城市、州和年份。(从文本文件中读取了城市、州和年份,但未在下面的代码中显示)

每当我在程序中输入“QUIT”并提示我进入一个城市时,它就会给我线程1错误,我不知道如何修复它

//HEADER FILE
class Data
{
private:
    string state;
    int year;
    string city;

public:
    //Constructor
    Data()
    { state = ""; year = 0; city = ""; }


    void setState(string s);
    void setYear(int y);
    void setCity(string c);
    string getState();
    int getYear();
    string getCity();
};

class City
{
private:
   // Declare a structure for the list
   struct ListNode
   {
      //Class declaration of data
      Data data;
      ListNode *next; // To point to the next node
   };

   ListNode *head; // List head pointer

public:
   //Constructor
   City()
    { head = NULL; }
   //Destructor
   ~City();

   // Linked list operations
   void insertNode(Data);
   void deleteNode(string);
   void displayList() const;
   void searchList(string);
 };


//OUT OF CLASS CPP FILE

void Data::setState(string s)
{
    state = s;
}

void Data::setYear(int y)
{
    year = y;
}

void Data::setCity(string c)
{
    city = c;
}

string Data::getState()
{
    return state;
}

int Data::getYear()
{
    return year;
}

string Data::getCity()
{
    return city;
}


/************************************************** 
   This function searches for the answer that 
   the user inputted from searchCity(). Traverses
   through the nodes to find userAnswer.
***************************************************/
void City::searchList(string userAnswer)
{
    ListNode *pNode;

    pNode = head;

    bool search = true;

    while(search)
    {
        if(pNode -> data.getCity() == userAnswer)
        {
            cout << pNode ->data.getState() <<" " <<pNode->data.getYear()
            <<" " <<pNode->data.getCity() <<endl;
            search = false;
        }
        else if( pNode->next == NULL )
        {
            if(userAnswer != "QUIT")
            {
                cout << userAnswer <<" was not found." <<endl;
            }

            search = false;
        }
        else if(userAnswer == "QUIT")
        {
            return;
        }

        pNode = pNode -> next;
    }

}
//**************************************************
// displayList shows the value                     *
// stored in each node of the linked list          *
// pointed to by head.                             *
//**************************************************

void City::displayList() const
{
   ListNode *nodePtr;  // To move through the list

   // Position nodePtr at the head of the list.
   nodePtr = head;

   // While nodePtr points to a node, traverse
   // the list.
    cout << left <<setw(15) << "STATE"  <<left << setw(15) 
    <<"YEAR" <<left   <<setw(15) <<"CITY" <<endl;
    cout << left <<setw(15) << "-----" <<left <<setw(15) 
    <<"-----" <<left <<setw(15) <<"-----" <<endl;

   while (nodePtr)
   {
      // Display the value in this node.
      //cout << nodePtr->value << endl;

      cout <<left <<setw(15)<< nodePtr->data.getState()  ;
      cout <<left <<setw(15)<< nodePtr->data.getYear()  ;
      cout <<left <<setw(15)<< nodePtr->data.getCity()   <<endl;

      // Move to the next node.
      nodePtr = nodePtr->next;
   }
}

//**************************************************
// The insertNode function inserts a node with     *
// Data copied.                 *
//**************************************************

void City::insertNode(Data dataIn)
{
   ListNode *newNode;             // A new node
   ListNode *nodePtr;             // To traverse the list
   ListNode *previousNode = NULL; // The previous node

   // Allocate a new node and store num there.
   newNode = new ListNode;
   //newNode->value = num;
   newNode->data = dataIn;

   // If there are no nodes in the list
   // make newNode the first node
   if (!head)
   {
      head = newNode;
      newNode->next = NULL;
   }
   else  // Otherwise, insert newNode
   {
      // Position nodePtr at the head of list.
      nodePtr = head;

      // Initialize previousNode to NULL.
      previousNode = NULL;

      // Skip all nodes whose value is less than num.
      //while (nodePtr != NULL && nodePtr->value < num)
      while (nodePtr != NULL && nodePtr->data.getCity() < dataIn.getCity())
      {
         previousNode = nodePtr;
         nodePtr = nodePtr->next;
      }

      // If the new node is to be the 1st in the list,
      // insert it before all other nodes.
      if (previousNode == NULL)
      {
         head = newNode;
         newNode->next = nodePtr;
      }
      else  // Otherwise insert after the previous node.
      {
         previousNode->next = newNode;
         newNode->next = nodePtr;
      }
   }
}


//**************************************************
// Destructor                                      *
// This function deletes every node in the list.   *
//**************************************************

City::~City()
{
   ListNode *nodePtr;   // To traverse the list
   ListNode *nextNode;  // To point to the next node

   // Position nodePtr at the head of the list.
   nodePtr = head;

   // While nodePtr is not at the end of the list...
   while (nodePtr != NULL)
   {
      // Save a pointer to the next node.
      nextNode = nodePtr->next;

      // Delete the current node.
      delete nodePtr;

      // Position nodePtr at the next node.
      nodePtr = nextNode;
   }
}

//IN MAIN
void readTextFile(City &list, Data Info);
void searchCity(City list);
void deleteCity(City list);

int main()
{

    City list;
    Data Info;

    readTextFile(list, Info);

   // Display the values in the list.

    list.displayList();

    searchCity(list);

    deleteCity(list);





   return 0;
}


 void readTextFile(City &list, Data Info)
{
    ifstream inputFile;

    string state;
    int year;
    string city;

    inputFile.open("cities.txt");

    if(inputFile.fail())
    {
        cout << "Unable to open text file. Closing program. " <<endl;
        exit(100);
    }


    while(inputFile >> state)
    {
        Info.setState(state);

        inputFile >> year;
        Info.setYear(year);

        inputFile.ignore(20,' ');

        getline(inputFile, city);
        Info.setCity(city);

        list.insertNode(Info);
    }

    inputFile.close();
}

void searchCity(City list)
{
    string userAnswer;

    while(userAnswer != "QUIT")
    {
        cout <<endl;
        cout << "Enter City Name or 'QUIT' to stop: ";
        getline(cin, userAnswer);

        list.searchList(userAnswer);
    }


}

您的问题在释放代码的某个地方,很可能(但不是100%)在析构函数中。您正在释放从未通过new或malloc创建的指针。我们需要查看您的完整数据和城市类以进行调试。啊,好的!我将把CPP文件的其余部分发布在上面的文本文件上。我无法确定和弄清楚编码。任何帮助都将不胜感激。:)我不同意@gabeschen,你发布的代码太多了。相反,坐下来,删除程序中不需要演示问题的所有内容。无论如何,读一下“三定律”,它会给你足够的提示来理解你的问题。不过,在你的程序的简化版本中理解这个问题会更容易,这也是为什么这里的发布指南要求(!)它的原因。@UlrichEckhardt,我说的时候他没有大部分代码it@BrandonNguyen实际上,为什么你有一个deleteCity函数呢?你的析构函数将遍历列表并释放它。当main退出时,会自动调用析构函数。那里没有必要做进一步的清理。如果该函数确实释放内存,那么您可能是在双重释放。