由於某些原因,當我在代碼中創建Student的對象時,構造函數被輸入許多次,我不知道爲什麼。我在構造函數和下面的代碼中放置了一個cout語句。爲什麼發生這種事情的任何幫助將是偉大的。自定義對象構造函數在循環外循環
//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") {...}
//我也在稍後收到分段錯誤核心轉儲。
更新由於某些原因,無論長我做我的列表,我的學生構造函數被輸入多次,這意味着我輸入30作爲我的列表中的長度構造函數輸入30倍,而不是隻創建一個30插槽的數組。這可能是什麼原因?我覺得如果過去我聽說過這個問題。
第一次構建時啓用了更多的警告(如果您還沒有得到任何信息),並且如果您有任何問題,請找出它們的含義以及您可以採取哪些措施來解決它們。然後閱讀[如何調試小程序](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)。 –
如果它是一個數組,它爲什麼稱它爲一個列表? – 2017-06-17 16:58:46
這是我的教授的代碼 –