C1001内部编译器错误是由于矢量初始化(如数组)引起的

C1001 internal compiler error due to vector initialization like arrays

本文关键字:数组 初始化 编译器 内部 错误 于矢量 C1001      更新时间:2023-10-16
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,现在我找不到问题所在。

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

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

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

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

弹出的是这样的:

这种不正确初始化的 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}}        }
};

演示

使用 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 + 选项-Wall -pedantic,它不会引发任何警告。与 gcc 8.3.0 相同

编辑 2:使用clang -Wall -pedantic,它也编译,没有任何警告。叮当 7.0.1-8

编辑3:出现带有Wextra选项的警告,如Ted Lyngmo所示

修改如下行:

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

该问题是由于对象创建期间未识别的值造成的。
尝试上面的代码,这将解决您的问题。记录对象不是 正确构建。

实际错误重现如下:

致命错误 C1001:编译器中发生内部错误。 1>(编译器文件 'd:\agent_work\2\s\src\vctools\compiler\utc\src\p2\main.c',第 187 行( 1> 要解决此问题,请尝试简化或更改 在上面列出的位置附近编程。

> 以下内容在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} }
};
}```