簡體   English   中英

使用構造函數創建類對象數組的問題,然后我想在 C++ 中向我的對象添加另一個值

[英]problem with create array of class object with constructor and then i want to add another value to my object in C++

使用構造函數創建類對象數組的問題,然后我想在 C++ 中向我的對象添加另一個值

我開了一門課,我想給它一個十六的啟動值,然后我可以手動添加教學知識。 當我想手動添加學生時程序崩潰

#include <iostream>
#include <string>

using namespace std;

class Student {
    int id;
    string name;
    float letter[5];

public:
    Student(int size) {
        id = size;
        name = "ali";

        for (int i = 0;i<5;i++) {
            letter[i] = 12;
        }
    }

    Student(int size,string Sname, float d[5]) {
        id = size;
        name = Sname;
        for (int i = 0; i < 5; i++) {
            letter[i] = d[i];
        }
    }

    void addStd(int size,string Sname) {
        id = size;
        name = Sname;

        for (int i = 0; i < 5; i++)
        {
            letter[i] = 15;
        }
    }

    void PrintStd() {
        cout << id << "\t" << name << "\t";
        for(int i=0;i<5;i++) {
            cout << letter[i] << "\t";
        }
        cout << endl;
    }
};


int main() {
    Student S1(1);
    float dd1[5] = {20,20,20,20,20};
    Student S2(2,"jack",dd1);

    Student s3[2] = {Student(1),Student(2,"fered",dd1)};

    for (int i=0; i < 2; i++) {
        s3[i].PrintStd();
        cout << "*************" << endl;
    }


    Student *s4;

    for(int i = 0; i < 2; i++) {
        s4=s3;
    }

    s4[3].addStd(2,"mary");


    cout << endl << endl << endl << endl;

    for (int i = 0; i < 2; i++) {
        s4[i].PrintStd();
        cout << "************" << endl;
    }

    return 0;
}

請告訴我。 謝謝

我建議你使用std::vectorstd::vector::emplace_back 您的主要功能將如下所示:

int main() {
    float dd1[5] = {20,20,20,20,20};

    std::vector<Student> students;
    students.emplace_back(1);
    students.emplace_back(2,"fered",dd1);

    for (auto& s : students) {
        s.PrintStd();
        cout << "*************" << endl;
    }

    students.emplace_back(2,"mary");

    cout << endl << endl << endl << endl;

    for (auto& s : students) {
        s.PrintStd();
        cout << "*************" << endl;
    }

    return 0;
}

查看實時示例

您工作中的一些問題是您試圖將新學生添加到現有的 Student 對象中,這在邏輯上根本不正確。 此外,您的第二個 for 循環不會遍歷整個數組(它迭代 2 次,但應該迭代 3 次)。

int main() {
    float dd1[5] = {20,20,20,20,20};

    Student S1(1);
    Student S2(2,"jack",dd1);

    Student s3[2] = {Student(1),Student(2,"fered",dd1)};

    for (int i=0; i < 2; i++) {
        s3[i].PrintStd();
        cout << "*************" << endl;
    }


    Student s4[3];

    // copying existing elements from s3 to new array s4
    for(int i = 0; i < 2; i++) {
        s4[i]=s3[i];
    }

    // creating 3rd student
    s4[2] = Student(2,"mary");

    cout << endl << endl << endl << endl;

    for (int i = 0; i < 3; i++) {
        s4[i].PrintStd();
        cout << "************" << endl;
    }

    return 0;
}

查看實時示例

暫無
暫無

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

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