简体   繁体   English

忽略GCC“错误:标量初始值设定项周围的花括号”类型错误。 向他们发出警告

[英]Ignoring GCC “error: braces around scalar initializer for type” errors. Make them warnings

I have a nagging problem with GCC compiler errors "error: braces around scalar initializer for type". 我对GCC编译器错误有一个na的问题:“错误:类型的标量初始化程序周围有花括号”。 I have seen others complaining about this, although they describe it as a warning ( gcc warning: braces around scalar initializer ) 我看到其他人对此表示抱怨,尽管他们将其描述为警告( gcc警告:标量初始值设定项周围的花括号

I am compiling code which is not mine to edit, and I get a lot of these errors throughout the code. 我正在编译不是我要编辑的代码,并且在整个代码中都遇到了很多这样的错误。

Basic Pattern is: 基本模式是:

struct t_
{
    float f;
    int i;
};

float f = { 0.3 };      //Compiler is all happy with this.
int i = {0};            //Compiler is all happy with this too.
t_ t1 = { 0.3, 0 };     //Compiler is all happy with this too.
t_ t2 = { {0.3}, 0 };   //Compiler ERROR: braces around scalar initializer for type 'float' 

I know I can remove the braces {} around the float scaler to remove this error, but I do not want to modify the code in any way. 我知道我可以删除浮点缩放器周围的花括号{}来消除此错误,但是我不想以任何方式修改代码。 Is there a flag I can give to GCC (currently using MinGW gcc 4.8.1). 我是否可以给GCC一个标志(当前使用MinGW gcc 4.8.1)。 ie "std=c++03", or something to get these errors at least displayed as warnings. 即“ std = c ++ 03”,或至少使这些错误显示为警告的内容。

Thanks 谢谢

I'm not 100% sure, but I believe there is no such option. 我不确定100%,但是我相信没有这种选择。 The construct you have is not meaning the same thing in the two cases - first one is an initialization of one structure, the second is aa strcuture containing a structure or array. 在两种情况下,您拥有的构造并不具有相同的含义-第一种是一种结构的初始化,第二种是包含结构或数组的结构。 Which of course float isn't. 这当然float不大。

You may be able to work around it with 您也许可以解决

struct t_
{
    struct 
    {
       float f;
    };
    int i;
};

At least clang is happy with that. 至少clang对此感到满意。 As is g++. 和g ++一样。 That may be easier than changing a lot of initialization statements with extra braces in them. 这可能比更改许多带有大括号的初始化语句要容易。 But it is admittedly still a change to the source code. 但是诚然,它仍然是对源代码的更改。 Unfortunately, I'm pretty certain that this is necessary. 不幸的是,我可以肯定这是必要的。

Complete example that I was testing with: 我正在测试的完整示例:

struct t_
{
    struct 
    {
        float f;
    };
    int i;
};



t_ t2 = { {0.3}, 0 };


int main()
{
    t2.f = 7;
}

Edit: If it's not at all possible to edit the source, you'll need to parse the source code, identify the incorrect braces and output "correct" code. 编辑:如果根本无法编辑源代码,则需要解析源代码,识别不正确的花括号并输出“正确”的代码。 The more I think about this, the less I believe that it's at all possible to fix without some sort of edit to the source. 我考虑的越多,我相信无需对源代码进行某种编辑就完全可以修复的可能性就越小。 Or that it has ever compiled... 或者它曾经编译过...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM