C++结构初始化断言失败
#include <cassert>
#include <string>
struct AStruct
{
int x;
char* y;
int z;
};
int main()
{
AStruct structu = {4, "Hello World"};
assert(structu.z == ???);
}
我应该写什么来代替 ???
才能成功断言?
我使用了 assert(structu.z == 0);
但不幸的是得到了错误int main(): 断言“structu.z == 0 failed.Aborted”
#include <cassert>
#include <string>
struct AStruct
{
int x;
char* y;
int z;
};
int main()
{
AStruct structu = {4, "Hello World"};
assert(structu.z == ???);
}
What should I write in place of ???
to have a successful assertion?
I used assert(structu.z == 0);
but unfortunately got the errorint main(): Assertion 'structu.z == 0 failed.Aborted'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您想要:
您的代码分配给 z 成员而不是测试它。如果您确实收到了编辑问题所说的消息,则说明您的编译器已损坏。是哪一个?
You want:
Your code assigns to the z member instead of testing it. And if you did get the message your edited question says you did, your compiler is broken. Which one is it?
assert(structu.z == 0)
应该可以工作,因为成员z
将会被初始化。assert(structu.z == 0)
should work because the memberz
would be value initialized.我假设您所说的“成功”是指不会创建错误消息。您可能想要:
请注意,我使用的是
==
,而不是=
。该断言永远不应该触发,因为使用给定的代码,
structu.z
保证等于0
。By "successful", I'm assuming you mean one that doesn't create an error message. You probably want:
Note that I'm using
==
, and not=
.This assertion should never trigger, because with the code given,
structu.z
is guaranteed to be equal to0
.