异构列表和动态数组分配
Heterogeneous list and dynamic array allocation
所以我在填充这个异构列表时遇到了问题。我传入一个文本文件,并使用它来填充对象的成员数据,然后将它们添加到列表中。这是我第一次使用它,我不明白为什么它不能正常工作。它应该将每个填充的对象地址保存到指针数组中的一个指针,对吗?然而,当我测试它时,我可以看到它只保存到(*list)[0]的单个位置。最相关的部分可能来自main.cpp。谢谢!
main.cpp
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include "student.h"
using namespace std;
int main()
{
string inputFileName;
string outputFileName;
cout << "Please enter the input file name: ";
getline(cin, inputFileName);
ifstream inputFile;
int NUMBEROFENTRIES;
inputFile.open(inputFileName.c_str()); //CLOSE
if(inputFile){
NUMBEROFENTRIES = int(inputFile.get())-int('0');
}else{
cout << "Failed to open file." << endl;
}
Student ** list; //create a pointer to Student pointer
list = new Student*[NUMBEROFENTRIES]; //dynamically allocated list of Student pointers
for(int i = 0; i < NUMBEROFENTRIES; i++)
{
string uselessNewLine;
getline(inputFile, uselessNewLine);
string tempfirstname;
string templastname;
getline(inputFile, templastname, ',');
inputFile.get();
getline(inputFile, tempfirstname);
char tempcourse = inputFile.get();
if(tempcourse == 'b'||tempcourse == 'B')
{
BioStudent bobj;
bobj.setNameAndCourse(tempfirstname, templastname, tempcourse);
string garbage;
getline(inputFile, garbage, ' ');
bobj.SetGrades(inputFile);
list[i] = &bobj; //I assume this is where the error is but i should be incrementing?
}
else if(tempcourse == 't' || tempcourse == 'T')
{
TheaterStudent tobj;
tobj.setNameAndCourse(tempfirstname, templastname, tempcourse);
string garbage;
getline(inputFile, garbage, ' ');
tobj.SetGrades(inputFile);
list[i] = &tobj; //I assume this is where the error is but i should be incrementing?
}
else if(tempcourse == 'c' || tempcourse == 'C')
{
CompsciStudent cobj;
cobj.setNameAndCourse(tempfirstname, templastname, tempcourse);
string garbage;
getline(inputFile, garbage, ' ');
getline(inputFile, garbage, ' ');
cobj.SetGrades(inputFile);
list[i] = &cobj; //I assume this is where the error is but i should be incrementing?
}else{
cout << "ERROR" << endl;
}
cout << (*list[0]).course << endl;
}
delete [] list;
return 0;
}
学生.h
#include <string>
#include <iostream>
using namespace std;
class Student
{
public:
virtual double GetAverage()=0; //Another pure virtual function
void setNameAndCourse(string fn, string ln, char tc);
Student();
char course;
char GetCourse();
protected:
string firstName;
string lastName;
private:
};
class BioStudent: public Student
{
public:
BioStudent();
void SetGrades(ifstream &input);
double GetAverage();
private:
int labGrade;
int test1;
int test2;
int test3;
int finalExam;
};
class TheaterStudent: public Student
{
public:
TheaterStudent();
void SetGrades(ifstream &input);
double GetAverage();
private:
int participation;
int midterm;
int finalExam;
};
class CompsciStudent: public Student
{
public:
CompsciStudent();
void SetGrades(ifstream &input);
double GetAverage();
private:
int assign1;
int assign2;
int assign3;
int assign4;
int assign5;
int assign6;
double assignAverage;
int test1;
int test2;
int finalExam;
};
学生.cpp
#include "student.h"
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
Student::Student()
{
firstName = "";
lastName = "";
course = ' ';
}
void Student::setNameAndCourse(string fn, string ln, char tc)
{
firstName = fn;
lastName = ln;
course = tc;
}
char Student::GetCourse()
{
return course;
}
BioStudent::BioStudent()
{
labGrade = 0;
test1 = 0;
test2 = 0;
test3 = 0;
finalExam = 0;
}
void BioStudent::SetGrades(ifstream& input)
{
input >> labGrade;
input >> test1;
input >> test2;
input >> test3;
input >> finalExam;
}
double BioStudent::GetAverage()
{
double toReturn = 0.0;
toReturn = ((labGrade*.3) + (test1*.15) + (test2*.15) + (test3*.15) + (finalExam*.25));
return toReturn;
}
TheaterStudent::TheaterStudent()
{
participation = 0;
midterm = 0;
finalExam = 0;
}
void TheaterStudent::SetGrades(ifstream &input)
{
input >> participation;
input >> midterm;
input >> finalExam;
}
double TheaterStudent::GetAverage()
{
double toReturn = 0.0;
toReturn = ((participation*.4)+(midterm*.25)+(finalExam*.35));
return toReturn;
}
CompsciStudent::CompsciStudent()
{
assign1 = 0;
assign2 = 0;
assign3 = 0;
assign4 = 0;
assign5 = 0;
assign6 = 0;
assignAverage = 0.0;
test1 = 0;
test2 = 0;
finalExam = 0;
}
void CompsciStudent::SetGrades(ifstream &input)
{
input >> assign1;
input >> assign2;
input >> assign3;
input >> assign4;
input >> assign5;
input >> assign6;
input >> test1;
input >> test2;
input >> finalExam;
}
double CompsciStudent::GetAverage()
{
double toReturn = 0.0;
assignAverage = ((assign1+assign2+assign3+assign4+assign5+assign6)/6);
toReturn = ((assignAverage*.3)+(test1*.2)+(test2*.2)+(finalExam*.3));
return toReturn;
}
你是对的,问题就在这里:
list[i] = &bobj; //I assume this is where the error is but i should be incrementing?
原因是CCD_ 1在堆栈上定义如下:
BioStudent bobj;
因此,一旦它的封闭范围结束,它就会被销毁,然后您的列表将包含一个悬空指针。
您想要的是动态分配的对象:
BioStudent* bobj = new BioStudent;
然后:
list[i] = bobj;
另外,不要忘记释放对象,或者最好使用智能指针。
相关文章:
- 在c++中为我自己的基于指针的数组分配内存的正确方法
- 具有自定义构造函数 (C++) 的类型的动态数组分配
- 如何通过 malloc 为队列数组分配内存?
- 具有动态数组分配的OpenMP嵌套循环
- 如何为数组结构的数组分配内存
- 如何为结构数组分配内存?
- cpp 数组 - 分配常量索引有效,而非常量索引不起作用
- 如何为字符指针数组分配内存?
- 在 C++ 中初始化后的数组分配
- 将数据分配给了数组分配,但程序显示了以缓冲区超支的输出
- C++函数问题中的数组分配
- 子数组分配:使用聚合对象错误的预期"{...}"进行初始化
- 为什么我的指针数组分配不起作用
- std::堆栈和堆栈溢出错误的数组分配
- 在 c++ 中是否有通过声明"implicit"数组分配?
- 在 c++ 中为 2-D "Dynamically Created"数组分配值时遇到问题
- 如何为C++数组分配指向第一个元素的指针
- 字符数组分配
- 将数组分配在固定数量的线程之间
- 为构造函数中的数组分配值