简体   繁体   English

C ++分段故障链表

[英]C++ Segmentation Fault linkedLists

I have a program that compiles perfectly fine, but when I try and run a function through my menu, I am just responded with segmentation fault. 我有一个可以很好地编译的程序,但是当我尝试通过菜单运行一个函数时,我只是遇到了段错误。 I can't quite seem to find where the seg fault is happening either. 我似乎也找不到发生段错误的位置。

linkedList.cpp linkedList.cpp

 #include "linkedList.h"
#include <iostream>
#ifndef LINKEDLIST_CPP
#define LINKEDLIST_CPP
using namespace std;
//Destructor
template<class T>
linkedList<T>::~linkedList()
{
  node *temp=new node;
  while(head->!=NULL)
    {
      temp=head;
      head=head->next;
      delete temp;
    }
}

//Copy Constructor
template <class T>
linkedList<T>::linkedList(const linkedList &list)
{
  node *temp =new node;
  temp =list.head;
  delete this;
  while(temp->next!=NULL)
    {
      orderedInsert(temp->data);
      temp=temp->next;
    }
}

//Assignment Operator
template <class T>
linkedList<T> &linkedList<T>::operator=(const linkedList &list)
{
  if(this != &list)
    {
      current =head;
      while(current->next!=NULL)
        {
          head=head->next;
          delete  current;
          current=head;
        }
      current=list.head;
      while(current->!=NULL)
        {
          orderedInsert(current->data);
          current=current->next;
        }
    }
  return *this;
  }

template <class T>
bool linkedList<T>::empty() const
{
  current=head;
 if(current==NULL)
    {
      return true;
    }
  else
    return false;
}
template <class T>
void linkedList<T>::clear()
{
  delete *this;
}

template <class T>
bool linkedList<T>::search(const T &value)
{
  current=head;
  while(current->data!=value||current->!=NULL)
    {
      current=current->next;
    }
  if(current->data==value)
    {
      return true;
    }
  else
    return false;
}

template <class T>
void linkedList<T>::orderedInsert(const T &value)
{
  current=head;
  while(value>current->data)
    {
      trailCurrent=current;
      current=current->next;
    }
  trailCurrent->next=new node(value,current);

}

template <class T>
bool linkedList<T>::remove(const T &value)
{
  node *temp;
  temp =head;
  if(search(value)!=true)
    {
      return false;
    }
  else
    {
    trailCurrent->next=current->next;
    temp=current;
    current=current->next;
    delete temp;
    return true;
    }
}
template <class T>
bool linkedList<T>::replace(const T &oldData,const T &newData)
{
  if(search(oldData)==false)
    {
      return false;
    }
  else
    {
      current=head;
      while(current->data!=oldData)
        {
          current=current->next;
        }
      current->data=newData;
    }
  return true;
}

template <class T>
void linkedList<T>::insert(const T &value)
{
  if(trailCurrent==NULL)
    {
      trailCurrent=head;
      head =new node(value,trailCurrent);
      trailCurrent=head;
    }
  else if(trailCurrent->next->data!=current->data)
    {
 trailCurrent=head;
      while(trailCurrent->next->data!=current->data)
        {
          trailCurrent=trailCurrent->next;
        }
      node *temp;
      temp=trailCurrent;
      temp=new node(value,current);
      trailCurrent->next=temp;
    }
  return true;
}

template <class T>
bool linkedList<T>::retrieve(T &value)const
{
  if(current==NULL)
    {
      return false;
    }
  else
    {
      current->data =value;
      return true;
    }
}

template <class T>
void linkedList<T>::begin()
{
 current=head;
  trailCurrent=NULL;
}

template <class T>
linkedList linkedList<T>::operator++()
{
  if(current!=NULL)
    {
      current=current->next;
      trailCurrent=trailCurrent->next;
      return *this;
    }
}

template<class T>
linkedList linkedList<T>::operator++(int i)
{
  if(current!=NULL)
    {
      trailCurrent=current;
      current=current->next;
    }
  return that;
}

template <class T>
ostream &operator<<(ostream &outStream,linkedList<T> list)
{
 T element;
this.current=this.head;
 while(this.current->!=NULL)
   {
     element=this.retrieve(element);
     outStream<<"["<<element<<"]"<<endl;
     this.current++;
   }
 return outStream;
}
#endif

There are a lot of nodes is .cpp and I can't seem to find where this seg fault is coming from. .cpp有很多节点,我似乎找不到该段错误的来源。

linkedListApp.cpp linkedListApp.cpp

#include "linkedList.h"
#include "myDate.h"
#include <iostream>
using namespace std;
#include <fstream>


//FillList
//Description: opens a file and fills the list from the file
//Parameters:  indexList
//Return:      none
void FillList(linkedList<myDate> &list);


//DisplayList
//Description: displays the list to the monitor
//Parameters: indexList
//Return:     none
void DisplayList(linkedList<myDate> list);

//AddToList
//Description: adds to the list by ordered insert
//Parameters: indexList,num
//Return:     none
void AddToList(linkedList<myDate> &list);

//GetTodaysAppointments
//Description: Asks the user for today's date, and looks for a matching date from the list
//Parameters: indexList,num
//Return: none
void GetTodaysAppointments(linkedList<myDate> list);

//ChangeAppointment
//Description: Lets the user choose an appointment to change, and changes as such
//Parameters: indexList
//Return: None
void ChangeAppointment(linkedList<myDate> &list);

//Menu
int menu();

int main()
{
        int choice;
        int num;
        linkedList<myDate> L;

        while ((choice = menu()) != 6)
        {
                switch (choice)
                {
                    case 1: FillList(L); break;
case 2: AddToList(L); break;
                    case 3: GetTodaysAppointments(L); break;
                    case 4: ChangeAppointment(L); break;
                    case 5: DisplayList(L); break;
                }
        }


        return 0;
}


//menu
int menu()
{
        int ch;
        cout << endl;
        cout << "1. Fill from file" << endl;
        cout << "2. Add to the list" << endl;
        cout << "3. Get Todays Appointments" << endl;
        cout << "4. Change an appointment" << endl;
        cout << "5. Display list" << endl;
        cout << "6. Quit"<<endl;
        cout << "Choice: ";
        cin >> ch;
        return ch;
}

//FillList
//Description: opens a file and fills the list from the file
//Parameters:  indexList
//Return:      none
void FillList(linkedList<myDate> &list)
{
  myDate aDate;
  myTime aTime;
  ifstream f;
  bool result =true;
  string fname;

  cout << "File: ";
  cin >> fname;
  f.open(fname.c_str());
  if (f.fail())
    {
      cout << "Failed to open" << endl;
      return;
    }
  list.begin();
  f >> aDate;
  while (result && !f.eof())
    {
      f>>aTime;
      aDate.setTime(aTime);
      list.orderedInsert(aDate);
      f>>aDate;

    }

  f.close();
}

Whenever I try to "fill" i just end up getting a seg fault, the rest of my code is here because I have more files. 每当我尝试“填充”时,我都会遇到段错误,其余代码在这里,因为我有更多文件。 But I think you can find the seg fault from these two I just cant 但我认为您可以从我无法找到的这两个中找到段错误

This is just some thoughts on the code - it is not conclusive and it's probably not the solution for the segfault. 这只是对代码的一些想法-它不是决定性的,并且可能不是segfault的解决方案。

You seem to misunderstand how to use pointers. 您似乎误解了如何使用指针。 For example in your destructor, you allocated memory for temp then immediately set it to head. 例如,在析构函数中,您为temp分配了内存,然后立即将其设置为head。 This is basically a leak. 这基本上是一个泄漏。 You can simply set it to head . 您只需将其设置为head There's no reason to allocate memory for it unless you plan on using it. 除非您计划使用它,否则没有理由为其分配内存。 You have this problem throughout your code. 您在整个代码中都有这个问题。 I would try to learn how pointers work first as that'll be fundamental to many other things. 我将尝试学习指针是如何首先工作的,因为这对于许多其他事情来说都是至关重要的。

template<class T>
linkedList<T>::~linkedList()
{
  node *temp=new node;
  while(head->!=NULL)
    {
      temp=head;
      head=head->next;
      delete temp;
    }
}

A potential problem in your search function: 搜索功能中的潜在问题:

while(current->data!=value||current->!=NULL)

You have the right idea here but the ordering is wrong. 您在这里有正确的想法,但订购是错误的。 What if current is null? 如果current为null怎么办? Take a look at short-circuit evaluation . 看一下短路评估

Your usage of current seems like a bad design for the many times that you use it but perhaps there's some requirement I don't know about. 您对current使用在许多次使用中似乎都是一个不好的设计,但是也许有些我不知道的要求。

There are a couple other things I noticed but if you need review of your code, there's other places to ask that. 我注意到了其他几件事,但是如果您需要检查代码,那么还有其他地方可以提出这一要求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM