Search code examples
c++loopsconstructorcoredump

Custom object constructor is looping outside of a loop


For some reason when I create an object of Student in my code the constructor is entered many many times and I am not sure why. I put a cout statement in the constructor and the code below. Any help of why this is happening would be great.

//Student.cpp
Student::Student() {
  ID = 0;
  name = "name";
  cout << "student constructor" << endl;
}

Student::Student(int id, string name) {
  ID = id;
  name = this->name;
  cout << "student con 2" << endl;
}




//part of SortedList.cpp just incase it is needed
template <class ItemType>
SortedList<ItemType>::SortedList() {
  cout << "In the default constructor" << endl;
  Max_Items = 50;
  info = new ItemType[Max_Items];
  length = 0;

//SortedList(50);//Using a default value of 50 if no value is specified                                                                                                              

}

//Constructor with a parameter given                                                                                                                                                 

template <class ItemType>
SortedList<ItemType>::SortedList(int n) {
  cout << "in the non default constructor" << endl;
  Max_Items = n;
  info = new ItemType[Max_Items];
  length = 0;
  cout << "At the end of the non default constructor" << endl;
}






 /The part of the driver where this is called
ifstream inFile;
  ofstream outFile;
  int ID; //what /below                                                                                                                                                              
  string name; //these werent here                                                                                                                                                   
  inFile.open("studcommands.txt");
  outFile.open("outFile.txt");
  cout << "Before reading commands" << endl;
  inFile >> command; // read commands from a text file                                                                                                                               
  cout << "After reading a command" << endl;
  SortedList<Student> list;//() was-is here                                                                                                                                          
  cout << "List has been made" << endl;
  Student StudentObj;
  cout << "Starting while loop" << endl;
  while(command != "Quit") {...}

//I am also getting a segmentation fault core dump a little after.

UPDATE For some reason however long I make my list, my student constructor is entered that many times meaning say I enter 30 as the length in my list the constructor is entered 30x instead of just creating an array with 30 slots. What could be the reason with this? I feel as If I have heard of this issue in the past.


Solution

  • In the SortedList constructor, you create a new array of the input size of ItemType objects. The elements of this array will be default constructed when the array is being built. That is why your Student constructor is being called size of the array times.