来,看几行 c++代码

2016-09-06 19:35:56 +08:00
 jccg90

问,下面这段代码有没有编译器警告,代码有没有问题,输出结果是多少? 求解答。。。

#include<stdio.h>

struct Info
{
    bool a;
    double b;
};

void getInfoList( vector<Info> & infoList)
{
    Info info;
    info.a = true;
    infoList.push_back(info);
}

int main()
{
    vector<Info> infoList;
    getInfoList( infoList);
    for (vector<Info>::iterator It = infoList.begin(); It != infoList.end(); It++)
    {
        printf("%f",(*It).b);
    }
    return 0;
}
5632 次点击
所在节点    程序员
42 条回复
bp0
2016-09-06 21:48:44 +08:00
好混搭的代码。
qua
2016-09-06 21:54:15 +08:00
info.b 没初始化啊
qua
2016-09-06 21:55:32 +08:00
这么简单的问题有什么好讨论的
owt5008137
2016-09-06 22:41:34 +08:00
有没有 warning 取决于你的编译选项,如果开全了 warning 的话会有 warning

另外,一定会编译 error ,原因如 @sgissb1 所说。虽然可以默认包含某些头文件,但是我所知的任何一个环境都不会自动 #include<vector>

最后问输出什么?这取决于你的编译器、编译选项、 runtime 库,还有硬件环境。所以你问会输出什么?鬼知道
kingddc314
2016-09-06 23:17:30 +08:00
局部变量的默认初始化具有不确定性,虽然这里打印是 0.0 ,但是后面再加上一个 int c; 成员后,打印的 b 却变成了随机值,应该是看编译器实现。所以不能依赖局部变量默认构造函数。全局变量的默认初始化则是零值了。

另外实在没编译器也可以试试在线编译器,如 http://webcompiler.cloudapp.net/
veelog
2016-09-06 23:20:49 +08:00
不知道这几行代码有什么意思。。。。
zby0826
2016-09-06 23:54:13 +08:00
看了这么多回复,也就 @jmc891205 正解……其他要么不会,要么挑刺,感觉 v 站 c++程序员太少。
josephshen
2016-09-07 00:38:41 +08:00
有些人不光是不会啊而且是胡说啊,回国之后 v 站水平下降不少啊
azh7138m
2016-09-07 00:54:51 +08:00
```

/tmp/480407296/main.cpp:9:6: error: variable has incomplete type 'void'
void getInfoList( vector<Info> & infoList)
^
/tmp/480407296/main.cpp:9:19: error: use of undeclared identifier 'vector'
void getInfoList( vector<Info> & infoList)
^
/tmp/480407296/main.cpp:9:26: error: 'Info' does not refer to a value
void getInfoList( vector<Info> & infoList)
^
/tmp/480407296/main.cpp:3:8: note: declared here
struct Info
^
/tmp/480407296/main.cpp:9:34: error: use of undeclared identifier 'infoList'
void getInfoList( vector<Info> & infoList)
^
/tmp/480407296/main.cpp:9:43: error: expected ';' after top level declarator
void getInfoList( vector<Info> & infoList)
^
;
5 errors generated.

```

https://glot.io/snippets/ei75xmvvui

楼主你开心就好:)
htfy96
2016-09-07 07:19:11 +08:00
其实 include stdio.h 就可以给你报编译错误…
LigeLaige
2016-09-07 08:24:41 +08:00
难道不需要 #include <vector> 吗?
KingHL
2016-09-07 09:39:37 +08:00
@yangff 恩,抱歉是我学艺不精说错了。
lzhCoooder
2016-09-07 09:50:59 +08:00
GCC 打开 -wall 试试
FifiLyu
2016-09-07 10:08:53 +08:00
jmc891205
2016-09-07 10:30:46 +08:00
@zby0826 现在程序员都不从 C/C++开始入门啦 都是从 Python 、 JavaScript 开始学 不懂这些东西也正常
araraloren
2016-09-07 11:05:47 +08:00
@jmc891205 楼主问问题的态度不怎么好吧。。
至少该说说自己到底是怎么搞的,遇到了什么问题。。
hitmanx
2016-09-07 13:38:53 +08:00
你可能需要这个网站:
http://coliru.stacked-crooked.com/

代码往里一贴,点击 compile and run
BlackKey
2016-09-07 16:15:43 +08:00
编译器没有义务去警告未定义行为
phttc
2016-09-07 17:02:36 +08:00
C++ 输出难道不是 cout 么。。。到底要看 C 还是 C ++
missdeer
2016-09-07 17:16:22 +08:00
作为 C++代码,第一行就没写好

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/304396

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX