【问题标题】:C/C++ declared and defined variable turns invisibleC/C++ 声明和定义的变量变为不可见
【发布时间】:2013-07-03 16:58:16
【问题描述】:

我的整个代码有点太多,无法在此处发布,因此我将尝试展示其中的要点。

我正在编写一个简单的图形表示的模拟时钟(三针 12 小时制)。 目前,如果我让时钟从默认值运行,即所有指针从 12 点开始,我的代码就可以工作。 但是,我添加了一项功能,允许编辑显示的时间和固有的时间,无论手的起始位置如何,当它达到 12 时,相应的较大手应该打勾一次。我的代码如下。

    for (psi = 0; psi<6.28318530718-0.5236; psi+=0.5235987756) {
        float xply = sin(psi);
        float yply = cos(psi);
        int hhx = x0 + (circleRad-100)*xply;
        int hhy = y0 - (circleRad-100)*yply;
        float phi;

        for (phi = 0; phi<6.28318530718-0.10472; phi+=0.1047197551) {
            float Multx = sin(phi);
            float Multy = cos(phi);
            int mhx = x0 + (circleRad-50)*Multx;
            int mhy = y0 - (circleRad-50)*Multy;
            float theta;

            for (theta= 0; theta<6.28318530718-0.104720; theta+=0.1047197551) {
                // If seconds are given then the if condition is tested

                if (secPhase > 0) {
                    float angle = theta+secPhase;

                    // If second hand reach top, for loop breaks and enters a new loop                          for next minute, secphase is erased as new minute start from 0 secs.

                    if (angle > 6.28318530718-0.104720) {
                        plotHands(angle, x0, y0, circleRad, a, mhx, mhy, hhx, hhy, bytes);
                        capture.replaceOverlay(true, (const unsigned char*)a);
                        sleep(1);
                        secPhase = 0;
                        break;
                    }

                    // if second hand has not reached top yet, then plotting continues

                    plotHands(angle, x0, y0, circleRad, a, mhx, mhy, hhx, hhy, bytes);
                    capture.replaceOverlay(true, (const unsigned char*)a);
                    sleep(1);
                }

                // if there were no seconds given, plotting begins at 12.

                else {
                    plotHands(theta, x0, y0, circleRad, a, mhx, mhy, hhx, hhy, bytes);
                    capture.replaceOverlay(true, (const unsigned char*)a);
                    sleep(1);
                }
            }
        }
    }

目前我的代码可以运行几秒钟。有声明和定义的值,我没有在这里包含,我可以改变它们,这将改变每只手的起始位置,无论秒针在哪里,当它达到 12 时,分针会滴答一次。

这就是问题所在。从逻辑上讲,我可以应用与秒针相同的概念,但将其迁移到分针并更改相关的相应变量名称,以便当分针敲击 12 时,时针会移动。这是中断的代码:

for (phi = 0; phi<6.28318530718-0.10472; phi+=0.1047197551) {
    if (minPhase > 0) {
        float minAngle = phi + minPhase;
        if (minAngle > 6.28318530718-0.10472) {
            minPhase = 0;
            break;
        }
        float Multx = sin(minAngle);
        float Multy = cos(minAngle);
        int mhx = x0 + (circleRad-50)*Multx;
        int mhy = y0 - (circleRad-50)*Multy;
    }
    else {
        float Multx = sin(phi);
        float Multy = cos(phi);
        int mhx = x0 + (circleRad-50)*Multx;
        int mhy = y0 - (circleRad-50)*Multy;
    }
}

我只采用了涉及分针的中间 for 循环。这些循环和语句确保如果没有给定的分针起点,则 else 语句将运行,但如果有起点,则起点将滴答作响,直到它敲打 12 点,它会中断到小时循环,滴答一次,同时清除分针的起点以在新的一小时重新开始。

但是,一旦我尝试编译代码,编译器就会告诉我:

错误:未在此范围内声明“mhx”
错误:未在此范围内声明“mhy”

每次在函数中调用此变量以绘制分针时,它都会显示这一点,就好像这些变量已经消失了一样。它们已在我的代码中明确声明和定义,当尝试在其下方的 for 循环中调用时,它声称这些变量丢失了。

我还发现,如果我删除了 'else' 语句,代码编译并运行,但被破坏了,即分针不在其应有的位置。

谁能给我指点一下?我对 C 和 C++ 还是很陌生。
提前谢谢你。

【问题讨论】:

  • 它们在块的右大括号处超出范围。

标签: c++ c variables loops clock


【解决方案1】:

当变量碰到 if 或 else 的右大括号时,它们会超出范围。在范围之外声明它们并在 if/else 块中分配它们的值。

for (phi = 0; phi<6.28318530718-0.10472; phi+=0.1047197551) {
    if (minPhase > 0) {
        float minAngle = phi + minPhase;
        if (minAngle > 6.28318530718-0.10472) {
            minPhase = 0;
            break;
        }
        float Multx = sin(minAngle);
        float Multy = cos(minAngle);
        int mhx = x0 + (circleRad-50)*Multx;
        int mhy = y0 - (circleRad-50)*Multy;
        // Multx, Multy, mhx, mhy will go out of scope when the following brace is reached
    }
    else {
        float Multx = sin(phi);
        float Multy = cos(phi);
        int mhx = x0 + (circleRad-50)*Multx;
        int mhy = y0 - (circleRad-50)*Multy;
        // Multx, Multy, mhx, mhy will go out of scope when the following brace is reached
    }
}

你应该这样做:

for (phi = 0; phi<6.28318530718-0.10472; phi+=0.1047197551) {
    float Multyx, Multy;
    int mhx, mhy;
    // These variables will now be visible in the entire for loop's scope not just the if or else statement they were declared into.
    if (minPhase > 0) {
        float minAngle = phi + minPhase;
        if (minAngle > 6.28318530718-0.10472) {
            minPhase = 0;
            break;
        }
        Multx = sin(minAngle);
        Multy = cos(minAngle);
        mhx = x0 + (circleRad-50)*Multx;
        mhy = y0 - (circleRad-50)*Multy;
    }
    else {
        Multx = sin(phi);
        Multy = cos(phi);
        mhx = x0 + (circleRad-50)*Multx;
        mhy = y0 - (circleRad-50)*Multy;
    }
}

【讨论】:

    【解决方案2】:

    您需要将mhxmhy 移动到if 语句上方的范围,以便在if/else 之外可见。

    for (phi = 0; phi<6.28318530718-0.10472; phi+=0.1047197551) {
        int mhx, mhy;  // move declaration here
        if (minPhase > 0) {
            float minAngle = phi + minPhase;
            if (minAngle > 6.28318530718-0.10472) {
                    minPhase = 0;
                break;
            }
            float Multx = sin(minAngle);
            float Multy = cos(minAngle);
            mhx = x0 + (circleRad-50)*Multx;  // no longer a declaration, just assignment
            mhy = y0 - (circleRad-50)*Multy;
        }
        else {
            float Multx = sin(phi);
            float Multy = cos(phi);
            mhx = x0 + (circleRad-50)*Multx;  // no longer a declaration, just assignment
            mhy = y0 - (circleRad-50)*Multy;
        }
    }
    

    我假设在您未显示的 if 语句之后,您的 for 循环体中还有其他代码。

    【讨论】:

      猜你喜欢
      • 2011-06-18
      • 1970-01-01
      • 1970-01-01
      • 2017-07-14
      • 2022-06-17
      • 2011-04-30
      • 2018-06-02
      • 1970-01-01
      相关资源
      最近更新 更多