通过 IF 语句选择结构

发布于 2024-11-17 01:15:20 字数 423 浏览 4 评论 0原文

我试图使用 IF 语句选择两个可能的 ANSI C 表达式之一。每个表达式本身都可以正常工作,例如...

wb_Parameters *WeightLimits = set100Parameters();

但是当通过 IF 选择它们时,例如...

if (strcmp(CurrentAircraft->PhenomType,"100") == 0) 
    wb_Parameters *WeightLimits = set100Parameters();
else
    wb_Parameters *WeightLimits = set300Parameters();

我收到错误消息“使用未声明的标识符‘WeightLimits’。”我需要做什么才能在 IF 语句中实现此功能?

I am trying to select one of two possible ANSI C expressions using an IF statement. Each expression works fine by itself, eg...

wb_Parameters *WeightLimits = set100Parameters();

but when they're selected via an IF eg...

if (strcmp(CurrentAircraft->PhenomType,"100") == 0) 
    wb_Parameters *WeightLimits = set100Parameters();
else
    wb_Parameters *WeightLimits = set300Parameters();

I get the error message "Use of undeclared identifier 'WeightLimits'." What do I need to do to make this work inside an IF statement?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧人 2024-11-24 01:15:20

WeightLimits 在 if 语句之后超出范围。为了避免这种情况,请在 if 之前声明它:

wb_Parameters *WeightLimits;
if (strcmp(CurrentAircraft->PhenomType,"100") == 0)
    WeightLimits = set100Parameters();
else
    WeightLimits = set300Parameters();

WeightLimits goes out of scope after the if statement. To avoid that, declare it before the if:

wb_Parameters *WeightLimits;
if (strcmp(CurrentAircraft->PhenomType,"100") == 0)
    WeightLimits = set100Parameters();
else
    WeightLimits = set300Parameters();
晨光如昨 2024-11-24 01:15:20

首先声明,然后分配正确的值:

wb_Parameters *WeightLimits = NULL;
if (strcmp(CurrentAircraft->PhenomType,"100") == 0) 
    WeightLimits = set100Parameters();
else
    WeightLimits = set300Parameters();

如果不先声明它就会超出范围并且不能再使用。

First declare and then assign the right value:

wb_Parameters *WeightLimits = NULL;
if (strcmp(CurrentAircraft->PhenomType,"100") == 0) 
    WeightLimits = set100Parameters();
else
    WeightLimits = set300Parameters();

If you don't first declare it it goes out of scope and it can not be used anymore.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文