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

Use of Undeclared Identifier "angle"?

我正在制作一个将直角坐标转换为极坐标的程序,每当我进入 运行 该程序时,它都会告诉我 "angle" 未声明,即使我确定我已经声明了它.我也知道程序没有返回任何东西,我现在只想 运行 它。

#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。因此错误。

您可能想继续阅读 scopes

你应该在你的 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);
}