警告:初始化程序周围缺少大括号

编程入门 行业动态 更新时间:2024-10-26 18:16:36
本文介绍了警告:初始化程序周围缺少大括号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 Hallo小组成员 你能告诉hw写这段代码是为了不接受来自主题的警告: #include< ; iostream> 使用命名空间std; 使用命名空间__gnu_cxx; struct entry { union { int a; }; union { int b; }; }; static struct entry en [] = {{12,13}}; int main(){ (void)en; }; 我在linux上编译它如下命令: g ++ -Wall -g -O0 -o / tmp / p2 p2.cpp 问候, Pawel

解决方案

Pawel写道: 可能是:

你好小组成员 你能告诉hw写这段代码是为了不接受来自主题的警告: #include< iostream> 使用命名空间std; 使用命名空间__gnu_cxx; struct entry {

union u_a { int a; };

union u_a {

int b; }; }; static struct entry en [] = {{12,13}}; int main(){ (void)en; }; 我使用以下命令在linux上编译它: g ++ -Wall -g -O0 -o / tmp / p2 p2.cpp 问候, Pawel

Pawel写道:

你好组成员 你能否告诉hw写这段代码为了不接受来自主题的警告: struct entry { union { int a; }; union { int b; }; }; static struct entry en [] = {{12,13}};

要静态初始化类型为entry的对象,您必须静态地使用初始化其每个字段。由于每个字段都是 聚合类型,编译器会警告您没有使用大括号 来标记这些字段的初始值设定项。在这种情况下它没关系,因为 每个字段只需要一个初始化程序。所以 第一个联盟初始化为12,第二个联盟初始化为13.如果你更改 那些联合体结构(当然是添加名称)初始化 仍然可以以相同的方式工作。但是如果你在第一个字段中添加一个字段,那么12和13将用于初始化它的两个字段,并且第二个结构将初始化为0。 。 所以,为了摆脱警告,在初始化程序周围加上大括号为 每个汇总成员: static struct entry en [] = {{{12},{13}}}; - - Pete 标准C ++库扩展:教程和参考的作者。有关本书的更多信息,请参阅 www.petebecker/tr1book。

Pawel写道:

你好组成员 你能告诉hw编写这段代码是为了不收到主题的警告: #include< iostream> 使用命名空间std; 使用命名空间__gnu_cxx; struct entry { union { int a; }; union { int b; }; }; static struct entry en [] = {{12,13}};

静态条目en [] = {{{12},{13}}};

int main(){ (void)en; };

删除最后一个分号。请注意,最后一个语句没有效果。 这是一件好事,因为: void n;是非法的 和en实际衰减到一个指针。所以你可以争辩说: int main() { void * pvoid = static_cast< void *>(& en); std :: cout<< pvoid = << pvoid<< std :: endl; }

> 我使用以下命令在linux上编译它: g ++ -Wall -g -O0 -o / tmp / p2 p2.cpp 问候, Pawel

Hallo group members Could You tell hw to write this code in order not to get warning from subject: #include <iostream> using namespace std; using namespace __gnu_cxx; struct entry { union { int a; }; union { int b; }; }; static struct entry en[] = { { 12, 13 } }; int main() { (void)en; }; I compile it on linux with following command: g++ -Wall -g -O0 -o /tmp/p2 p2.cpp regards, Pawel

解决方案

Pawel wrote: May be:

Hallo group members Could You tell hw to write this code in order not to get warning from subject: #include <iostream> using namespace std; using namespace __gnu_cxx; struct entry {

union u_a{

int a; };

union u_a{

int b; }; }; static struct entry en[] = { { 12, 13 } }; int main() { (void)en; }; I compile it on linux with following command: g++ -Wall -g -O0 -o /tmp/p2 p2.cpp regards, Pawel

Pawel wrote:

Hallo group members Could You tell hw to write this code in order not to get warning from subject: struct entry { union { int a; }; union { int b; }; }; static struct entry en[] = { { 12, 13 } };

To statically initialize an object of type entry you have to statically initialize each of its fields. Since each of those fields is in turn an aggregate type, the compiler is warning you that you haven''t used braces to mark the initializers for those fields. In this case it''s okay, since there''s only one initializer needed for each of those fields. So the first union is initialzied with 12 and the second with 13. If you change those unions to structs (adding names, of course) the initialization would still work the same way. But if you then added a field to the first one, the 12 and 13 would be used to initialize its two fields, and the second struct would be initialized to 0. So, to get rid of the warning, put braces around the initializer for each of the aggregate members of entry: static struct entry en[] = { { {12}, {13} } }; -- -- Pete Author of "The Standard C++ Library Extensions: a Tutorial and Reference." For more information about this book, see www.petebecker/tr1book.

Pawel wrote:

Hallo group members Could You tell hw to write this code in order not to get warning from subject: #include <iostream> using namespace std; using namespace __gnu_cxx; struct entry { union { int a; }; union { int b; }; }; static struct entry en[] = { { 12, 13 } };

static entry en[] = { { {12},{13} } };

int main() { (void)en; };

remove that last semicolon. Note that the last statement has no effect. Which is a good thing since: void n; is illegal and en actually decays to a pointer. So you could argue: int main() { void* pvoid = static_cast<void*>(&en); std::cout << "pvoid = " << pvoid << std::endl; }

> I compile it on linux with following command: g++ -Wall -g -O0 -o /tmp/p2 p2.cpp regards, Pawel

更多推荐

警告:初始化程序周围缺少大括号

本文发布于:2023-07-31 15:46:33,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:括号   初始化   程序

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!