我有一些非常奇怪的行为代码导致编译器崩溃。我正在使用 VC++ 17:
//.hpp
typedef unsigned short UID;
typedef UID GoodType;
typedef struct _Recipe
{
typedef struct {
GoodType goodType; unsigned short units;
} GoodRatio;
std::vector<GoodRatio> input;
GoodRatio output;
//int a;
} Recipe;
//.cpp
int main()
{
std::vector<Recipe> recipes
{
{
{ { 0, 1 }, { 1, 2 } }, { 2, 1 }
},
};
}
错误:
1>c:\users\peter\downloads\nationsgamemockup\nationsgamemockup\nationsgamemockup.cpp(25): fatal error C1001: An internal error has occurred in the compiler.
1>(compiler file 'f:\dd\vctools\compiler\utc\src\p2\main.c', line 255)
1> To work around this problem, try simplifying or changing the program near the locations listed above.
到目前为止我观察到的行为:
如果我取消注释 //int a;
无论我是否在主函数的构造函数调用中为 a 添加参数,错误都会消失。
如果我评论 std::vector<GoodRatio> input;
行并从构造函数调用中删除相应的参数,错误消失。
如果我评论 GoodRatio output;
行并从构造函数调用中删除相应的参数,错误消失。
如果我更改 recipes
main
中的变量到一个recipe
错误消失了(就像简单的 Recipe a{ { { 0, 1 },{ 1, 2 } },{ 2, 1 } };
一样)。
注意:
_Recipe
struct 不能是匿名结构,因为它有一个 std::vector<GoodRatio>
类型的数据成员和 Recipe::GoodRatio
type 仅在 typedef 之后的分号处出现。至少这是我的猜测。如果我尝试这样做,我会收到错误消息。
编辑:我的问题是:为什么会这样?
最佳答案
向 GoodRatio 添加构造函数似乎可以解决问题:
GoodRatio(unsigned short goodType = {}, unsigned short units = {}) :
goodType{ goodType }, units{ units } {}
我也向微软发送了反馈。
关于c++ - VC++ 17 内部编译器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45132490/