struct record
{
    double mid_exam;
    double fin_exam;
    double assignment[5];
    double score;
    char grade;
};

struct student
{
    string name;
    record math;
    record science;
};

int main()
{
    vector<student> students {
        // { name, math, science}
        { "John", {10.5, 90, 80, 85, 20, 70, 60}, {70.5, 90, 80, 85, 20, 70, 60} },
        { "Elton", {20.5, 90, 80, 85, 20, 70, 60}, {70.5, 90, 80, 85, 20, 70, 60} },
        { "Houston", {30.5, 90, 80, 85, 20, 70, 60}, {70.5, 90, 80, 85, 20, 70, 60} },
        { "Ashton", {40.5, 90, 80, 85, 20, 70, 60}, {70.5, 90, 80, 85, 20, 70, 60} },
        { "Lee", {50.5, 90, 80, 85, 20, 70, 60}, {70.5, 90, 80, 85, 20, 70, 60} },
        { "Jack", {60.5, 90, 80, 85, 20, 70, 60}, {70.5, 90, 80, 85, 20, 70, 60} },
        { "Christiano", {70.5, 90, 80, 85, 20, 70, 60}, {70.5, 90, 80, 85, 20, 70, 60} },
        { "Lukas", {80.5, 90, 80, 85, 20, 70, 60}, {70.5, 90, 80, 85, 20, 70, 60} },
        { "Sahid", {90.5, 90, 80, 85, 20, 70, 60}, {70.5, 90, 80, 85, 20, 70, 60} },
        { "Ryan", {90.5, 90, 80, 85, 20, 70, 60}, {70.5, 90, 80, 85, 20, 70, 60} }
    };
}

我正在为学生制作带有 vector 的成绩管理程序。

我初始化了类似 vector 的数组。我没有写分数和年级数字,这将在年级计算功能中进行计算。

Visual Studio打印错误消息,编译器内部发生内部错误。

首先,我用

student students[10]

数组,它工作正常。

我只将array更改为vector,但现在找不到错误所在。

最佳答案

主要问题是您没有在初始化程序列表中用大括号括住assignment,但这不会导致编译器崩溃。

  • 单击Visual Studio中的Feedback图标(右上角)。
  • 单击Report a problem
  • 在搜索字段中,放入

  • 单击与搜索匹配的链接。它应该是第一场比赛,它是我报告的。

  • 弹出的是这样的:

    初始化不正确的std::vector会使编译器崩溃。
    #include <string>
    #include <vector>
    
    struct record
    {
        double mid_exam;
        double fin_exam;
        double assignment[5];
        double score;
        char grade;
    };
    
    struct student
    {
        std::string name;
        record math;
        record science;
    };
    
    int main()
    {
        std::vector<student> students{
            { "John", {10.5, 90, 80, 85, 20, 70, 60}, {70.5, 90, 80, 85, 20, 70, 60}},
            { "Elton", {20.5, 90, 80, 85, 20, 70, 60}, {70.5, 90, 80, 85, 20, 70, 60}}
        };
    }
    

    解决方法很简单。我只是正确地初始化它。
    std::vector<student> students{
        { "John", {10.5, 90, {80, 85, 20, 70, 60}, 0, 0}, {70.5, 90, {80, 85, 20, 70, 60}, 0, 0}},
        { "Elton", {20.5, 90, {80, 85, 20, 70, 60}, 0, 0}, {70.5, 90, {80, 85, 20, 70, 60}, 0, 0}}
    };
    

    您也可以对这个问题进行投票。

    如果您不想在初始值设定项列表中输入scoregrade,另一种方法是添加record的构造函数,并使用std::array而不是常规数组:
    #include <array>
    
    struct record
    {
        record(double m, double f, const std::array<double, 5>& a) :
            mid_exam(m), fin_exam(f), assignment(a), score{}, grade{}
        {}
        record() : record({}, {}, {}) {} // default constructor, delegating to the above
        double mid_exam;
        double fin_exam;
        std::array<double, 5> assignment;
        double score;
        char grade;
    };
    

    这样可以初始化它:
    std::vector<student> students{
        {"John", {10.5, 90, {80, 85, 20, 70, 60}}, {70.5, 90, {80, 85, 20, 70, 60}}},
        {"Elton", {20.5, 90, {80, 85, 20, 70, 60}}, {70.5, 90, {80, 85, 20, 70, 60}}        }
    };
    

    Demo

    关于c++ - 由于数组之类的 vector 初始化,C1001内部编译器错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59780951/

    10-11 22:13
    查看更多