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
,现在我找不到问题所在。
主要问题是您没有在初始值设定项列表中用大括号括起来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}}
};
您也可以对该问题进行投票。
如果您不想在初始值设定项列表中输入score
和grade
,另一种方法是为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} }
};
}```
- C++使用整数的压缩数组初始化对象
- C++17中函数模板中的静态数组初始化(MSVC 2019)
- 从另一个静态常量数组初始化静态常量数组(只需少量计算)
- 在 Python 和 c++ 2d 数组初始化之间.这是怎麽?为什么呢?
- 字节数组初始化会导致 DirectX 崩溃
- 使用 new 和 值进行数组初始化,但没有显式数量的元素
- 运行时C++数组初始化问题
- 使用带有参数包的数组的成员数组初始化类
- 仅通过C++中数组初始化的不同方法,即可在同一输入上获得两个不同的答案
- C++ 2 个指针数组初始化 C2440
- C++结构字符数组初始化
- C++中的多维数组初始化
- constexpr数组初始化
- C++引物动态数组初始化程序的数目超过大小
- 使用std::index_sequence对std::数组初始化进行包扩展
- 当 std 数组初始化太小时,C++会引发错误吗?
- 如何确定结构数组初始化的大小?
- Qt并发错误:数组初始化需要大括号括起来的初始值设定项列表
- 此代码中的数组初始化样式是什么?这是标准的吗?
- C++ 使用数组初始化时的 STL 向量内存管理