简体   繁体   English

当 C++ 中的函数通过引用调用时,变量超出了 main 的作用域

[英]Variables out of scope in main when called by reference by a function in C++

I have made the following code, whose output should generate a point uniformly at random on the unit circle centered at the origin:我编写了以下代码,其输出应在以原点为中心的单位圆上随机均匀地生成一个点:

#include "unif.h"
#include <iostream>
#include <cmath>
using namespace std;

void point_on_circle(double& x, double& y)
{
 double r;

do
    {
    double x = unif(-1.,1.);
    double y = unif(-1.,1.);
    double r = x*x + y*y;
    }
    while (r >=1.0);

    x = x / sqrt(r);
    y = y / sqrt(r);
}

int main()
{

 cout << "Pair of points on the circle found is " << x << " and " << y << endl;
 cout << "Let's verify: x^2+y^2=" << x*x+y*y << endl;

    return 0;
}

The header "unif.h" is just a file that contains a function void unif(double x, double y), that produces uniformly random numbers in the interval (x,y), and it works perfectly (already tested).头文件“unif.h”只是一个包含函数 void unif(double x, double y) 的文件,它在区间 (x,y) 中产生均匀的随机数,并且它完美地工作(已经测试过)。 The problem is that when I build the program then it gives me (of course) the error in the main:问题是,当我构建程序时,它给了我(当然)主要的错误:

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

which is clear since of course x is defined outside the main and never defined in main().这很清楚,因为当然 x 是在 main 之外定义的,而从未在 main() 中定义。 I cannot figure out how to tell the compiler that the values of x and y found by the function point_on_circle should be "carried" inside the main.我不知道如何告诉编译器函数 point_on_circle 找到的 x 和 y 的值应该在 main 中“携带”。 How could I fix this code?我怎么能修复这个代码? Thanks in advance提前致谢

In your main method you did not declare a variable called x nor y .在您的 main 方法中,您没有声明一个名为xy的变量。 Moreover, you also have scoping issues in your point_on_circle(double& x, double& y) function with the variable r .此外,您的point_on_circle(double& x, double& y)函数中的变量r也存在范围界定问题。

Please review C++ scoping. 请查看 C++ 范围。

Because you defined x in the do-while loop, so you cannot use it outside the loop, since those definitions hide the parameters x and y.因为您在 do-while 循环中定义了 x,所以您不能在循环外使用它,因为这些定义隐藏了参数 x 和 y。 Define it before the loop:在循环之前定义它:

void point_on_circle(double& x, double& y)
{
    double r;

    do
    {
        x = unif(-1.,1.);
        y = unif(-1.,1.);
        r = x*x + y*y;
    }while (r >=1.0);

    x = x / sqrt(r);
    y = y / sqrt(r);
}

You have a few issues.你有几个问题。 1) you need to declare x and y inside main. 1) 你需要在 main 中声明 x 和 y。 2) you never, ever actually call point_on_circle. 2)你从来没有真正调用过point_on_circle。 At all.在所有。 3) finally, as others noted, you mask parameters x and y in your do loop. 3)最后,正如其他人指出的那样,您在 do 循环中屏蔽了参数 x 和 y。

With all of that said, it looks like you're attempting to find a random point on the unit circle.综上所述,看起来您正在尝试在单位圆上找到一个随机点。 with that in mind, I would remove the do loop entirely and just do this:考虑到这一点,我会完全删除 do 循环,然后执行以下操作:

void point_on_circle(double& x, double& y)
{
    double r;
    x = unif(-1.,1.);
    y = unif(-1.,1.);
    r = x*x + y*y;
    x = x / sqrt(r);
    y = y / sqrt(r);
}

It gives the exact same result while avoiding a (potential) endless loop, and certainly avoids useless extra processing.它给出了完全相同的结果,同时避免了(潜在的)无限循环,当然也避免了无用的额外处理。

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

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