fork download
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class Parent // Holds the info for each node.
  6. {
  7. private:
  8. string name;
  9. int age;
  10. string gender; // Will be specified in child class.
  11. Parent *next;
  12.  
  13. public:
  14. Parent(string newname, int newage)
  15. {
  16. name = newname;
  17. age = newage;
  18. }
  19.  
  20. void setGender(string myGender) { gender = myGender; }
  21. void setNext(Parent *n) { next = n; }
  22. };
  23.  
  24. class Child : public Parent // Create instances to store name, age, and gender.
  25. {
  26. public:
  27. Child(string newname, int newage, string newgender) : Parent(newname, newage)
  28. {
  29. setGender(newgender);
  30. }
  31. };
  32.  
  33. class Group // Linked list containing all Parent/Child.
  34. {
  35. private:
  36. int count;
  37. Parent *top;
  38. Parent *bottom;
  39.  
  40. public:
  41. Group()
  42. {
  43. count = 0;
  44. top = bottom = NULL;
  45. }
  46.  
  47. void addChild(); // Defined in the function listed at the top of this post.
  48. };
  49.  
  50. void Group::addChild() // Adding an instance of a child class.
  51. {
  52. Parent *now = bottom, *temp;
  53. string name = "Jason", gender = "male";
  54. int age = 21;
  55.  
  56. temp == new Child(name, age, gender);
  57.  
  58. if (count == 0)
  59. {
  60. top = bottom = temp;
  61. temp->setNext(NULL); // Segmentation fault?
  62. count++;
  63. }
  64. }
  65.  
  66. int main()
  67. {
  68. Group group;
  69.  
  70. cout << "Checkpoint 1." << endl;
  71.  
  72. group.addChild(); // Segmentation Fault.
  73.  
  74. cout << "Checkpoint 2." << endl;
  75.  
  76. return 0;
  77. }
  78.  
Runtime error #stdin #stdout 0s 3232KB
stdin
Standard input is empty
stdout
Checkpoint 1.