簡體   English   中英

分段錯誤C ++導致程序崩潰

[英]Segmentation fault c++ causing program to crash

我正在嘗試運行以下代碼,但是一旦創建了繼承類的對象,它就會崩潰。

該程序可以正常工作,直到創建繼承的類對象為止,但是在執行“ Student * s”行之后出現分段錯誤。

這是代碼:

#include <iostream>
#include <vector>

using namespace std;

class Person
{
protected:
    string firstName;
    string lastName;
    int id;
public:
    Person(string firstName, string lastName, int identification)
    {
        this->firstName = firstName;
        this->lastName = lastName;
        this->id = identification;
    }
    void printPerson()
    {
        cout << "Name: " << lastName << ", " << firstName << "\nID: " << id
                << "\n";
    }

};

class Student: public Person
{
private:
    vector<int> testScores;
public:
    Student(string f, string l, int i, vector<int>& scores) :
            Person(firstName, lastName, id)
    {
        this->firstName = f;
        this->lastName = l;
        this->id = i;
        this->testScores = scores;
    }

    char calculate()
    {
        vector<int>::iterator it;
        int sum = 0;
        for (it = testScores.begin(); it != testScores.end(); it++)
        {
            sum += *it;
        }
        int avg = sum / (testScores.size());
        if (avg >= 90 && avg <= 100)
            return 'O';
        else if (avg >= 80 && avg < 90)
            return 'E';
        else if (avg >= 70 && avg < 80)
            return 'A';
        else if (avg >= 55 && avg < 70)
            return 'P';
        else if (avg >= 40 && avg < 55)
            return 'D';
        else
            return 'T';

    }
};

int main()
{
    string firstName;
    string lastName;
    int id;
    int numScores;
    cin >> firstName >> lastName >> id >> numScores;
    vector<int> scores;
    for (int i = 0; i < numScores; i++)
    {
        int tmpScore;
        cin >> tmpScore;
        scores.push_back(tmpScore);
    }
    cout << "Calling\n";
    Student* s = new Student(firstName, lastName, id, scores);
    cout << "Done\n";
    s->printPerson();
    cout << "Grade: " << s->calculate() << "\n";
    return 0;
}
    Student(string f, string l, int i, vector<int>& scores) :
            Person(firstName, lastName, id)

使用Person自己的firstNamelastNameid成員構造Person 這可能會,也可能不會爆發,但肯定會進入不確定的行為。

OP需要更多類似這樣的東西,其中使用傳遞的參數代替:

    Student(string f, string l, int i, vector<int>& scores) :
            Person(f, l, i)
    {
        // all of the members that were set here don't need to be set. 
        // Person did it already
        this->testScores = scores;
    }

可以進行另一項改進。 可以使用成員初始化程序列表初始化testScores 這樣可以將scores立即填充到testScores ,並且編譯器可以進行各種有趣的優化。

    Student(string f, string l, int i, vector<int>& scores) :
            Person(f, l, i), testScores(scores)
    {
    }

盡可能在初始化列表中初始化成員變量。 `人員也可以得到改善:

    Person(string first, string last, int identification):
        firstName(first),
        lastName(last),
        id(identification)
    {
    }

現在可以找到並修復Rakete1111所發現的除以零的bug。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM