在此范围内未声明"y",

'y' was not declared in this scope,

本文关键字:未声明 范围内      更新时间:2023-10-16

我对这个项目有很多问题,我想出了很多。 但是这个我不能

在这里我有一些计算,然后我需要这个"y"变量传递给下一个.cpp

#include <iostream>
#include <math.h>
using namespace std;
int Ing(int number, float y)
{
y = 0;
float Lngth = 0;
for(; number != 0; number /= 10, Lngth++);
float n = nearbyint(sqrt(Lngth));
y = Lngth * pow(10, n);
return  (y);
}

这是下一个.cpp

#include <iostream>
#include "InitialGuess.h"
#include <math.h>
using namespace std;
int SquareRoot(int number, int th)
{
float iGuess = Ing(y);
float x = iGuess;
 for (int k=1; k< th; ++k)
    {
        x = (x + (number / x ))/2; 
} 
cout<<x;    
return (x);
}

但是在编译时,它给了我错误,即"y"未在此范围内声明。我哪里犯了错误?

谢谢

在此行

float iGuess = Ing(y);

您没有声明 Y,这会导致错误。你想传递给 Ing() 什么值?

您为 Ing(int, float) 定义了 2 个参数,但只用一个参数调用它。

y = 0;

应该是

int y = 0;

因为此时尚未声明y并且您正在定义一个变量。