There is a code, at rantayme gives a segmentation error:

#include <iostream> using namespace std; class StudentList { private: class Student { private: string name; int age; double height; public: Student *next; explicit Student(string n,int a,double h): name(n), age(a), height(h) {} string getName() { return name; } int getAge() { return age; } double getHeight() { return height; } ~Student() {} }; Student *temp; public: explicit StudentList() { temp=NULL; } ~StudentList() { } void list() { Student* student=temp; while(student) { cout<<temp->getName()<<"\t"<<temp->getAge()<<"\t"<<temp->getHeight()<<"\n"; temp=temp->next; } //delete student; } void insertValue(string name,int age,double height) { Student *student=new Student(name,age,height); student->next=temp; temp=student; } }; int main() { StudentList ls; ls.insertValue("Falcao",45,1.87); ls.insertValue("Jacob",97,3.87); ls.list(); } 

The problem is in the function sheet ..

1 answer 1

You have the student variable in the list method checked in a loop, but it does not change. Instead, the temp field changes. In theory, you need to replace temp=temp->next; for something like student=student->next; . This is what immediately catches the eye. And so that there is no memory leak (seg fault is not a leak, it is an attempt to access memory, to which the program has no access), you need to write a destructor for the StudentList class. So he took turns removing all students.