由于像数组这样的向量初始化导致 C1001 内部编译器错误

C1001 internal compiler error due to vector initialization like arrays

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} }
    };
}

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

我初始化了类似向量的数组。分数和年级号我没写,会在年级计算函数中计算出来的。

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

起初,我用

制作了这段代码
student students[10]

阵列,它工作正常。

我只把array改成了vector,我现在找不到问题了。

以下在 VS2015 中为我工作:

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()
{
     student students[10] =  {
        // { 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} }
    };
}```

使用gcc (7.4.0),没有编译内部错误,但代码不被接受。它通过简单地识别不同的初始化级别来工作,正如评论中已经提到的那样:

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} } }
    };
}

编辑:这段代码仍然没有用 VS 编译(见评论)。但是,使用 gcc + options -Wall -pedantic,它不会发出任何警告。同 gcc 8.3.0

编辑 2:使用 clang -Wall -pedantic,它也可以编译,没有任何警告。铿锵声 7.0.1-8

编辑 3:显示 Wextra 选项的警告,如 Ted Lyngmo

所示

主要问题是您没有在初始化列表中用大括号括起 assignment,但这不应该使您的编译器崩溃。

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

    Internal error during IMAGE::BuildImage with improperly initialized std::vector

  • 点击link匹配搜索。应该是第一场比赛,是我举报的

弹出的是这个:

初始化不当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

修改如下行:

{ "John", record{10.5, 90, {80, 85, 20, 70, 60}, 0, ' '}, record{70.5, 90, {80, 85, 20, 70, 60}, 0 , ' '} }

问题是由于对象创建过程中的未识别值造成的。
试试上面的代码,这将解决您的问题。记录对象不是 得到正确构建。

实际错误重现如下:

fatal error C1001: An internal error has occurred in the compiler. 1>(compiler file 'd:\agent_work\s\src\vctools\compiler\utc\src\p2\main.c', line 187) 1> To work around this problem, try simplifying or changing the program near the locations listed above.