C++ 自定义对象构造函数正在循环外部循环
由于某种原因,当我在代码中创建一个Student对象时,构造函数被输入了很多次,我不知道为什么。我在构造函数中放了一个cout语句和下面的代码。任何关于为什么会发生这种情况的帮助都将是巨大的C++ 自定义对象构造函数正在循环外部循环,c++,loops,constructor,coredump,C++,Loops,Constructor,Coredump,由于某种原因,当我在代码中创建一个Student对象时,构造函数被输入了很多次,我不知道为什么。我在构造函数中放了一个cout语句和下面的代码。任何关于为什么会发生这种情况的帮助都将是巨大的 //Student.cpp Student::Student() { ID = 0; name = "name"; cout << "student constructor" << endl; } Student::Student(int id, string name
//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") {...}
//Student.cpp
学生::学生(){
ID=0;
name=“name”;
cout在SortedList
构造函数中,您创建一个新的数组,数组的输入大小为ItemType
对象。此数组的元素将在构建数组时默认构建。这就是为什么会调用学生
构造函数的数组时间大小。第一次构建时会有更多警告启用(如果你还没有得到),如果你得到了,那么找出它们的含义以及你可以做些什么来修复它们。然后阅读。如果它是一个数组,为什么称它为列表?这是我的教授给我的代码啊,好吧,这是有道理的,出于某种原因,我觉得内存中的空间被打开了。谢谢!