简体   繁体   English

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

[英]C/C++ declared and defined variable turns invisible

The entirety of my code is a bit too much to post on to here so I'll try to show the essentials. 我的代码的全部内容太多了,无法在此处发布,因此我将尝试展示其要点。

I am coding a simple graphically represented analogue clock (12-hour with three hands). 我正在编写一个简单的图形表示的模拟时钟(三只手12小时)。 Currently my code works if I let the clock run from default ie all hands start at 12. However I have added a feature that allows editing of the time shown and inherent to this, regardless of starting position of the hand, when it hits 12, the larger respective hand should then tick once. 目前,如果我让时钟从默认值开始运行(即所有指针都从12开始),我的代码就可以工作。但是,我添加了一项功能,该功能允许编辑显示的时间以及此时间所固有的时间,无论指针的起始位置是12还是12。然后,较大的相应手应打勾一次。 My code is below. 我的代码如下。

    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);
                }
            }
        }
    }

Currently my code works for seconds. 目前,我的代码可以运行几秒钟。 There are declared and defined values, that I have not included here, that I can alter that will change the starting position of each hand and wherever the second hand is, when it hits 12 the minute hand will tick once. 这里有一些声明和定义的值,我没有在其中包括,我可以进行更改,以更改每只手的起始位置,无论秒针在哪里,当秒针达到12时,分针都会打勾一次。

This is the problem. 这就是问题。 Logically, I could just apply the same concept that I used for the second hand but migrate it to the minute hand and change the respective variable names involved so that when the minute hand does strike 12, the hour hand will move. 从逻辑上讲,我可以应用与秒针相同的概念,但是将其迁移到分针并更改涉及的各个变量名称,以便当分针碰到12时,时针就会移动。 This is the code that breaks: 这是破裂的代码:

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;
    }
}

I have taken only the middle for loop involving the minute hand. 我只涉及涉及分针的中圈。 These loops and statements ensure that if there is no given starting point of the minute hand, the else statement will run, but if there is a starting point, the starting point will tick until it strikes twelve and which point it breaks to the hour for loop, ticks once, whilst clearing the starting point of the minute hand to start afresh in the new hour. 这些循环和语句确保了如果没有给定的分针起点,则else语句将运行,但是如果有起点,则起点将滴答作响,直到达到12点为止,并且该点将中断至小时循环,滴答一次,同时清除分针的起点以在新的小时重新开始。

However once I attempt to compile the code, the compiler tells me: 但是,一旦我尝试编译代码,编译器就会告诉我:

error: 'mhx' was not declared in this scope 错误:未在此范围内声明“ mhx”
error: 'mhy' was not declared in this scope 错误:在此范围内未声明“ mhy”

it shows this everytime this variable is called in the function to draw the minute hands and is as if these variables have simply disappeared. 它每次在函数中调用此变量以绘制分针时都显示此状态,就好像这些变量只是消失了一样。 They have clearly been declared and defined in my code by when attempted to be called in the for loop below it, it claims that these variables are missing. 当试图在其下面的for循环中调用它们时,已在我的代码中明确声明和定义了它们,并声称缺少这些变量。

I found also that if I removed the 'else' statement, the code compiled and run, but was broken, ie the minute hand was not in its supposed position. 我还发现,如果删除了'else'语句,则代码会编译并运行,但是会损坏,即分针不在其应有的位置。

Can anyone enlighten me please? 谁能启发我? I am still very new to C and C++. 我对C和C ++还是很陌生。
Thank you in advance. 先感谢您。

The variables go out of scope when they hit the closing brace of either the if or the else. 当变量达到if或else的结尾时,它们超出范围。 Declare them outside of the scope and assign their values inside the if/else blocks. 在范围外声明它们,并在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
    }
}

You should instead do this: 您应该改为:

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;
    }
}

You need to move mhx and mhy to the scope above the if statement to be visible outside the if/else. 您需要将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;
    }
}

I assume you have other code in the body of your for loop after this if statement that you haven't shown. 我假设在未显示的if语句之后,您的for循环主体中还有其他代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM