我正在尝试编写一个名为Student的类,该类同时具有参数化的和默认的构造函数,并且该参数化的版本可以正常工作,但是在我手动分配值后尝试运行默认构造函数时,控制台崩溃。

Student.cpp:

Student::Student()
{
    this -> firstName = firstName;
    this -> lastName = lastName;
    this -> maxGrades = maxGrades;
    grades[maxGrades];
}

Student::Student(string fName, string lName, int mGrades)
{
    firstName = fName;
    lastName = lName;
    maxGrades = mGrades;
    grades[maxGrades];
}

Student::~Student()
{

}

void Student::setFirstName(string fName)
{
    firstName = fName;
}

void Student::setLastName(string lName)
{
    lastName = lName;
}

void Student::setMaxGrades(int mGrades)
{
    mGrades = maxGrades;
    grades[maxGrades];
}

string Student::getFirstName()
{
    return firstName;
}

string Student::getLastName()
{
    return lastName;
}

void Student::addGrade(int currentGradeNumber, double addedGrade)
{
    if(currentGradeNumber < maxGrades)
    {
        grades[currentGradeNumber] = addedGrade;
        cout << "grade " << currentGradeNumber << "is " << grades[currentGradeNumber] << endl;
    }
}

double Student::calcAvg()
{
    double sum = 0;
    double avg = 0;
    for(int i=0; i < maxGrades;i++)
    {
        sum += grades[i];
    }
    avg = sum/maxGrades;
    return avg;
}


studentTest.cpp:

int main()
{
    Student student1("Bill", "Nye", 3);
    cout << "First Name: " << student1.getFirstName() << endl;
    cout << "LastName: " << student1.getLastName() << endl;
    student1.addGrade(0, 90);
    student1.addGrade(1, 95);
    student1.addGrade(2, 80);
    cout << "Average is " << student1.calcAvg() << endl;

    Student student2;
    student2.setMaxGrades(2);
    student2.setFirstName("Frank");
    student2.setLastName("West");
    cout << "\nFirst Name: " << student2.getFirstName() << endl;
    cout << "Last Name: " << student2.getLastName() << endl;
    student2.addGrade(0,50);
    student2.addGrade(1,100);
    cout << "Average is: " << student2.calcAvg();
    return 0;
}


student.h:

class Student
{
    private:
        string firstName;
        string lastName;
        int maxGrades;
        int numGrades;
        double grades[];
    public:
        Student();
        Student(string, string, int);
        ~Student();
        void setFirstName(string);
        void setLastName(string);
        string getFirstName();
        string getLastName();
        void addGrade(int, double);
        double calcAvg();
        void setMaxGrades(int);
};


student1对象运行良好,但是当我尝试对student2使用addGrade()或calcAvg()时,就会出现错误。任何帮助,将不胜感激。

最佳答案

当您的两个构造函数都这样做时,它们是不正确的:

grades[maxGrades]; // This does not do what you think it does


这行不会在参数化的构造函数中崩溃,因为maxGrades具有已知值。但是,您的默认构造函数重用了未初始化的maxGrades值,从而导致未定义的行为。

您应该使用初始化列表重写构造函数。假设gradesstd::vector<int>,则可以这样操作:

Student::Student() : maxGrades(0)
{
// The remaining members will be initialized, because they have constructors.
}

Student::Student(string fName, string lName, int mGrades)
:   firstName(fName)
,   lastName(lName)
,   maxGrades(mGrades)
,   grades(mGrades, 0)
{
}

关于c++ - 如何在同一个类中同时具有参数化构造函数和默认构造函数?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20510323/

10-16 21:03