在循环外声明的变量在 C++ 中是无法识别的
Variable declared outside of a loop is unidentified inside C++
我有一个用 Microsoft Visual Studio 编写的 C++ 程序(我刚刚开始学习)。这是我的代码:
else
// time is in seconds
int time = 0;
double speed = 0.0;
while (height >= 0)
{
cout << "At " << int time << " secs the ball is at height: " << height << " metres.\n";
time++;
height -= distanceTravelled(speed);
speed += gravity;
}
// If height dropped from positive to negative in one second, the final fraction of a
// second before it hits the ground isn't displayed - so the if statement
if (height <= 0)
cout << "At " << time << " secs the ball is at height: " << height << " metres.\n";
当我尝试构建它时出现错误
"time" is an undeclared identifier.
但我已经在 while 循环之外声明了它。那为什么找不到呢?
问题是您在 cout 语句中声明了一个新变量:
cout << "At " << int 时间 << " 秒球处于高度:" << 高度 << " 米。\n";
只需删除 int
你的问题就在这里:
else //<==== missing paranthesis
// time is in seconds
int time = 0;
double speed = 0.0;
else 后面缺少左括号。实际发生的是 else 之后的第一个语句是 if-else 语句的假分支。之后发生的不是。因此,double speed = 0.0; 行之后的所有代码都在 if 语句之外,这在您的代码摘录中是不可见的。
这实际上使 int time 处于一个完整的其他范围,而不是进一步向下访问它的代码。这就是为什么代码访问 int 时间变量,找不到。
解决方法:在 else 之后添加一个 {,然后再往下添加一个 } 以涵盖您的逻辑。
您发布的代码中有两个问题。一个是输出线上的虚假 int
。应该是这样的:
cout << "At " << time << " secs the ball is at height: " << height << " metres.\n";
第二个问题是您的 else
缺少大括号。这意味着只有 time
的声明在 else
分支内,其他所有内容都与条件处于同一级别(缩进在 C++ 中不算数)。所以它应该是这样的:
else
{
// time is in seconds
int time = 0;
double speed = 0.0;
while (height >= 0)
{
cout << "At " << time << " secs the ball is at height: " << height << " metres.\n";
time++;
height -= distanceTravelled(speed);
speed += gravity;
}
// If height dropped from positive to negative in one second, the final fraction of a
// second before it hits the ground isn't displayed - so the if statement
if (height <= 0)
cout << "At " << time << " secs the ball is at height: " << height << " metres.\n";
}
这一行有问题。
cout << "At " << int time << " secs the ball is at height: " << height << " metres.\n";
int time
在这里应该只替换为 time
。
只有在定义变量时才使用变量指定数据类型,如 int time
,或在强制转换时,如 (int)time
。您只是在打印一个 int
变量。
我无法重现您在使用 g++ 编译器时遇到的完全相同的错误,但更改以上内容可能会解决问题。
我有一个用 Microsoft Visual Studio 编写的 C++ 程序(我刚刚开始学习)。这是我的代码:
else
// time is in seconds
int time = 0;
double speed = 0.0;
while (height >= 0)
{
cout << "At " << int time << " secs the ball is at height: " << height << " metres.\n";
time++;
height -= distanceTravelled(speed);
speed += gravity;
}
// If height dropped from positive to negative in one second, the final fraction of a
// second before it hits the ground isn't displayed - so the if statement
if (height <= 0)
cout << "At " << time << " secs the ball is at height: " << height << " metres.\n";
当我尝试构建它时出现错误
"time" is an undeclared identifier.
但我已经在 while 循环之外声明了它。那为什么找不到呢?
问题是您在 cout 语句中声明了一个新变量:
cout << "At " << int 时间 << " 秒球处于高度:" << 高度 << " 米。\n";
只需删除 int
你的问题就在这里:
else //<==== missing paranthesis
// time is in seconds
int time = 0;
double speed = 0.0;
else 后面缺少左括号。实际发生的是 else 之后的第一个语句是 if-else 语句的假分支。之后发生的不是。因此,double speed = 0.0; 行之后的所有代码都在 if 语句之外,这在您的代码摘录中是不可见的。
这实际上使 int time 处于一个完整的其他范围,而不是进一步向下访问它的代码。这就是为什么代码访问 int 时间变量,找不到。
解决方法:在 else 之后添加一个 {,然后再往下添加一个 } 以涵盖您的逻辑。
您发布的代码中有两个问题。一个是输出线上的虚假 int
。应该是这样的:
cout << "At " << time << " secs the ball is at height: " << height << " metres.\n";
第二个问题是您的 else
缺少大括号。这意味着只有 time
的声明在 else
分支内,其他所有内容都与条件处于同一级别(缩进在 C++ 中不算数)。所以它应该是这样的:
else
{
// time is in seconds
int time = 0;
double speed = 0.0;
while (height >= 0)
{
cout << "At " << time << " secs the ball is at height: " << height << " metres.\n";
time++;
height -= distanceTravelled(speed);
speed += gravity;
}
// If height dropped from positive to negative in one second, the final fraction of a
// second before it hits the ground isn't displayed - so the if statement
if (height <= 0)
cout << "At " << time << " secs the ball is at height: " << height << " metres.\n";
}
这一行有问题。
cout << "At " << int time << " secs the ball is at height: " << height << " metres.\n";
int time
在这里应该只替换为 time
。
只有在定义变量时才使用变量指定数据类型,如 int time
,或在强制转换时,如 (int)time
。您只是在打印一个 int
变量。
我无法重现您在使用 g++ 编译器时遇到的完全相同的错误,但更改以上内容可能会解决问题。