0

プログラムの動作がおかしくなり、クラッシュしました。

クラス「Student」のオブジェクトへのポインタの配列を使用して、クラス「List」を作成しました。'List'コンストラクターで'Student'オブジェクトを正常に呼び出したことを確認します。しかし、他の「List」メソッドから「Student」オブジェクトを呼び出すことはできません。

'List'コンストラクターと'push'メソッド内でテストするために同じ行をチェックしたところ、プログラムがクラッシュしました。

テストラインは次のとおりです。cout << (studentBox[numb] -> getRef()) << endl;

コードの問題のある部分は次のとおりです。

#include <iostream>

using namespace std;

class Student
{
    private:
        int referenceNumb;
        int markNumb;
    public:
        Student();
        Student(int, int);
        ~Student();
        int getRef();
        int getMark();
};

class List
{
    private:
        int numb;
        Student* studentBox[1000];
    public:
        List();
        ~List();
        void push(int, int);
        int getAVG();
};

int main()
{
    List* base = NULL;
    int x, y;
    char mod;
    do
    {
        cout << "Waiting for orders." << endl;
        cout << "1 - Quit," << endl;
        cout << "2 - Add new student," << endl;
        cout << "3 - Calculate average mark," << endl;
        cout << "0 - Create new list." << endl;
        cin >> mod;

        switch(mod)
        {
            case '0': base = new List(); break;
            case '1': cout << "Bye."; break;
            case '2':
                if(base != NULL)
                {
                    cout << "Specify student's reference number: "; cin >> x;
                    cout << "Specify student's mark: "; cin >> y;
                    base->push(x, y);
                }
                else cout << "List does not exist!" << endl;
                break;
            case '3':
                if(base != NULL)
                {
                    cout << "Average mark is equal: " << base->getAVG();
                }
                else cout << "List does not exist!";
                cout << endl;
                break;
            default: cout << "Correct command required!" << endl; break;
        }
    }
    while(mod!='1');
    return 0;
}

Student::Student()
{
    referenceNumb = NULL;
    markNumb = NULL;
}

Student::Student(int r, int m)
{
    referenceNumb = r;
    markNumb = m;
}

Student::~Student()
{
    referenceNumb = NULL;
    markNumb = NULL;
    cout << "pusto." << endl;
}

int Student::getRef()
{
    return referenceNumb;
}

int Student::getMark()
{
    return markNumb;
}

List::List()
{
    int numb = 0;
    studentBox[numb] = new Student();

    cout << (studentBox[numb] -> getRef()) << endl;
}

List::~List()
{
}

void List::push(int x, int y)
{
    cout << (studentBox[numb] -> getRef()) << endl;

    if(studentBox[numb] != NULL)
    {
        studentBox[numb] = new Student();

        cout << (studentBox[numb] -> getRef()) << endl;
    }
    else cout << "Hujnia" << endl;
}

int List::getAVG()
{
    int temp = 0;
    for(int i=0; i<numb; i++)
    {
        temp += studentBox[i]->getMark();
    }

    return (temp / numb);
}
4

1 に答える 1

0

私が見る大きな問題がいくつかあります。1 つ目はnumb、List のメンバーが初期化されていないことです。

List::List()
{
    int numb = 0;  // this creates a new local variable called numb, it doesn't
                   // initialize the numb member.
    studentBox[numb] = new Student();

    cout << (studentBox[numb] -> getRef()) << endl;
}

代わりにこれを行います:

List::List()
  : numb(0) // initialize the numb member to zero.
{
    studentBox[numb] = new Student();

    cout << (studentBox[numb] -> getRef()) << endl;
}

もう 1 つの問題はnumb、リストのサイズを示すことを意図しているように見えますが、新しい生徒が追加されても決して変更されないことです。

于 2012-12-16T20:50:22.647 に答える