C/C++声明和定义的变量变为不可见

C/C++ declared and defined variable turns invisible

本文关键字:变量 C++ 声明 定义      更新时间:2023-10-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;
    }
}

我只在涉及分针的循环中选择了中间部分。这些循环和语句确保,如果没有给定的分针起始点,else语句将运行,但如果有起始点,起始点将滴答作响,直到它达到十二点,循环的哪个点中断到小时,滴答作响一次,同时清除分针的起始点,以便在新的小时重新开始。

然而,一旦我试图编译代码,编译器告诉我:

错误:未在此作用域中声明"mhx"
错误:未在此作用域中声明"mhy"

每当函数中调用这个变量来绘制分针时,它就会显示这一点,就好像这些变量已经消失了一样。在我的代码中,当试图在下面的for循环中调用它们时,它声称这些变量丢失了。

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

有人能启发我吗?我对C和C++还很陌生
提前谢谢。

当变量碰到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;
    }
}

您需要将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循环的主体中还有其他代码,但您尚未显示。