异构列表和动态数组分配

时间:2016-07-19 22:14:57

标签: c++ pointers dynamic-arrays dynamic-allocation heterogeneous

所以我在填充这个异构列表时遇到了问题。我传入一个文本文件并使用它来填充对象的成员数据,然后将它们添加到列表中。这是我第一次使用这个,我无法理解为什么它不能正常工作。它应该将每个填充的对象地址保存到指针数组中的指针,对吗?但是当我测试它时,我可以看到它只保存到(* 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;
}

student.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;
};

student.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;
}

1 个答案:

答案 0 :(得分:0)

你是对的,问题出在这里:

 list[i] = &bobj; //I assume this is where the error is but i should be incrementing?

原因是因为bobj在堆栈上定义如下:

 BioStudent bobj;

所以一旦它的封闭范围结束它就会被销毁,然后你的列表会有一个悬空指针。

你想要的是动态分配的对象:

BioStudent* bobj = new BioStudent;

然后:

list[i] = bobj;

也不要忘记释放你的对象,或者更好地使用智能指针。