使用未声明的标识符"angle"?

Use of Undeclared Identifier "angle"?

本文关键字:angle 标识符 未声明      更新时间:2023-10-16

我正在制作一个将直角坐标转换为极坐标的程序,每当我运行该程序时,它都会"告诉"我"角度"是未声明的,尽管我确信我已经声明了它。我也知道该程序不会返回任何东西,我现在只想能够运行它。

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <cmath>
using namespace std;
double random_float(double min, double max);
void rect_to_polar(double x, double y, double &distance, double &angle);
int main() {
    double x, y;
    x = random_float(-1, 1);
    y = random_float(-1, 1);
    rect_to_polar(x, y, distance, angle);
}
double random_float(double min, double max) {
    unsigned int n = 2;
    srand(n);
    return ((double(rand()) / double(RAND_MAX)) * (max - min)) + min;
}

void rect_to_polar(double x, double y, double &distance, double &angle) {
    const double toDegrees = 180.0/3.141593;
    distance = sqrt(x*x + y*y);
    angle = atan(y/x) * toDegrees;
}

您没有在main()中声明任何名为angle的内容,但仍在其中使用名称angle。因此出现了错误。

你可能想了解一下作用域。

您应该在main中声明distanceangle

int main() {
    double x, y, angle, distance;
    x = random_float(-1, 1);
    y = random_float(-1, 1);
    rect_to_polar(x, y, distance, angle);
}