未初始化的局部变量

时间:2016-07-14 04:57:08

标签: c++

我刚刚学习C ++并遇到了这个问题,我花了一个小时试图修复和研究答案,但我似乎无法弄清楚我做错了什么。我使用Visual Studios作为我的IDE,这是最新版本。

#include "stdafx.h"
#include <iostream>
#include "constant.h"

//height of the tower
double towerHeight(double x)
{
    using namespace std;
        cout << "Enter a height for the tower" << '\n';
        cin >> x;

        return x;
}

//the number of seconds since the ball has been dropped to determine the distance
double secondsSinceDrop(double x)
{
    using namespace std;
        cout << "How long has it been since you dropped the ball (Seconds): ";
        cin >> x;

        return x;
}

//finds how far off the ground the ball is 
double currentBallHeight(double x, double y)
{
    return y * constant::gravity - x;
}

//prints how far off the ground the ball is
void printResult(double x, double y)
{
    using namespace std;
        if (currentBallHeight(x, y) < 0)
            cout << "At " << y << " the ball is on the ground." << '\n';
        else
            cout << "At " << y << " the ball is at: " << currentBallHeight(x, y) << '\n';
}

int main()
{
    double x = towerHeight(x);
    double y = secondsSinceDrop(x);

    printResult(x,y);

    return 0;
}

这是错误代码 - 第2章综合测验(第2部分).cpp(46):错误C4700:未初始化的局部变量&#39; x&#39;使用

-Line(46)是 - double x = towerHeight(x);

我已经得到了这个并且我已经改变了我的代码以将其归结为这一个错误,但我无法弄清楚如何修复它。它可能是一件简单的东西,我很愚蠢地忽视它,但任何帮助都会非常感激。

4 个答案:

答案 0 :(得分:1)

main()中的第一行代码是double x = towerHeight(x);,当您尚未初始化时,您发送给该函数的x值是多少。 当你使用变量而没有初始化它时,它是未定义的。

您可以将变量作为对函数的引用传递,并接受其中的值。

//height of the tower
void towerHeight(double &x)
{
    using namespace std;
    cout << "Enter a height for the tower" << '\n';
    cin >> x;
}

//the number of seconds since the ball has been dropped to determine the distance
void secondsSinceDrop(double &y)
{
    using namespace std;
    cout << "How long has it been since you dropped the ball (Seconds): ";
    cin >> y;
}

int main()
{
    double x = 0.0, y = 0.0;
    towerHeight(x);
    secondsSinceDrop(y);
    printResult(x, y);
    return 0;
}

答案 1 :(得分:1)

  

这些行会抛出错误   
因为变量&#39; x&#39;您作为参数发送的内容不存在于主

的范围内
 int main()
 {
 ->  double x = towerHeight(x);
 ->  double y = secondsSinceDrop(x);

     printResult(x,y);

     return 0;
 }

相反,你可以尝试这样的事情。

#include "stdafx.h"
#include <iostream>
#include "constant.h"
using namespace std;

//height of the tower
double towerHeight()
{
    double height;
    cout << "Enter a height for the tower" << '\n';
    cin >> height
    return height;
}

//the number of seconds since the ball has been dropped to determine the distance
double secondsSinceDrop()
{
    double seconds;
    cout << "How long has it been since you dropped the ball (Seconds): ";
    cin >> seconds;
    return seconds;
}

//finds how far off the ground the ball is 
double currentBallHeight(double x, double y)
{
    return y * constant::gravity - x;
}

//prints how far off the ground the ball is
void printResult(double x, double y)
{
    if (currentBallHeight(x, y) < 0)
        cout << "At " << y << " the ball is on the ground." << '\n';
    else
        cout << "At " << y << " the ball is at: " << currentBallHeight(x, y) << '\n';
}

int main()
{
    double height = towerHeight();
    double seconds = secondsSinceDrop();

    printResult(height, seconds);

    return 0;
}

我建议的一些提示

  
      
  • 尽可能多地声明您的变量,而不是使用&#39; x / y / z&#39;
  •   
  • 无需在每个函数
  • 中添加 using namespace std;   

答案 2 :(得分:1)

当你

时,你似乎正在努力将计算机正在做的事情连接起来
  • 声明具有初始值的变量
  • 定义函数参数
  • 从函数返回值

不确定这个问题如何与SO社区公平对待Q / A的优点是简洁和可重用(可能有些编辑可以帮助)但是为了您的利益,让我解释一下这些概念。

让我们从变量声明开始

int x = 5;
int y = x;

定义int x;时,它会在RAM中为整数(4个字节)创建一个空格。添加= 5会立即对其进行初始化。重要的是,在计算机尝试为x腾出空间之前,=右侧的值(本例中为5)是已知的。

对于像这样的变量,使用非常量的值是很好的(注意示例中的第二行),但在声明{{1}之前必须知道x }。换句话说,这显然是一个问题:

y

出于同样的原因,行int y = x; int x = 5; 存在问题,因为在定义double x = towerHeight(x);之前调用x时您使用的是towerHeight

定义函数的参数时:

x

这告诉计算机您要将值从double towerHeight(double x) { 中的值复制到RAM中的新位置并将其称为“towerHeight”。这意味着函数外部的值不会被修改。请考虑以下示例:

x

即使double towerHeight(double x) { x = 5; std::cout << x << std::endl; // outputs 5 } int main() { double x = 10; towerHeight(x); std::cout << x << std::endl; // outputs 10 return 0; } 中的x已更改为towerHeight的“x”,但也恰好称为同名。

从函数返回值时,与传递函数参数的方式相同,返回值将被复制并在函数调用的位置使用。让我们稍微修改前面的例子:

double towerHeight(double x) {
    x = 5;
    return x;
}
int main() {
    double x = 10;
    x = towerHeight(x); // returns the value "5"
    std::cout << x << std::endl; // Outputs "5"
    return 0;
}

您可以认为towerHeight(x)被“5”取代,因此代码会显示为x = 5;

结论

您应该尝试为

使用不同的变量名称
  • 函数参数(传递给函数的变量/值)
  • 函数参数(在函数内部调用它们)

避免这种混乱。虽然有时候使用相同名称是有意义的(即通过引用传递,这是另一个问题)。了解真实情况对您来说很重要。

以下是可能打算做的事情:

double towerHeight()
{
    double height;
    std::cout << "Enter a height for the tower" << std::endl;
    std::cin >> height;
    return height;
}
double secondsSinceDrop()
{
    double seconds;
    std::cout << "How long has it been since you dropped the ball (Seconds): ";
    std::cin >> seconds;
    return seconds;
}
double currentBallHeight(double y0, double t)
{
    return y0 - (constant::gravity * t * t / 2);
}
void printResult(double y0, double t)
{
    double currentHeight = currentBallHeight(y0, t);
    if (currentHeight < 0)
        std::cout << "At " << t << "s the ball is on the ground." << std::endl;
    else
        std::cout << "At " << t << "s the ball is at: " << currentHeight << std::endl;
}
int main()
{
    double y0 = towerHeight();
    double t = secondsSinceDrop();

    printResult(y0, t);

    return 0;
}

总结我改变了什么:

  • x重命名为y0,因为y(0) / h(0)通常用于物理类中的“初始高度”,类似y t (虽然time会是一个更好的名字)。
  • 不要将任何内容传递给towerHeightsecondsSinceDrop;你不是试图给这些功能一些东西,而是试图从中获取一些东西。
  • x的定义从函数参数移动到towerHeightsecondsSinceDrop
  • 函数中定义的局部变量
  • 删除了对currentBallHeight的重复调用(无需进行两次相同的数学运算,毕竟需要时间来处理数字,无论这种情况多么小)
  • 重写以正确使用std::coutstd::endl
  • 重写currentBallHeight等式以匹配常数自由落体运动学(y(t) = y(0) - 0.5g * t^2)作为额外奖励(假设constant::gravity > 0

在某些时候,了解我在此处概述的概念的更多技术术语和定义对您来说很有价值。以下是一些推荐读物(仅为了让您入门;始终保持学习):

答案 3 :(得分:0)

重写您的功能如下:

//height of the tower
double towerHeight()
{
        double x;
        using namespace std;
        cout << "Enter a height for the tower" << '\n';
        cin >> x;

        return x;
}

并在int main(){}重写以下行:

double x = towerHeight();

我想这样做但你可以用这种方式修改你的double secondsSinceDrop(double x);函数,因为它实际上不需要double值作为参数。

相关问题