C++ 'y' 未在此范围内声明,

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19371693/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 22:45:19  来源:igfitidea点击:

'y' was not declared in this scope,

c++compilation

提问by Yuriy Mikhailovich

I had a lot of problems with this project, I figured out a lot. but this one I can't

我在这个项目中遇到了很多问题,我想出了很多。但这个我不能

Here I have some calculations, and then I need this "y" variable to pass to next .cpp

这里我有一些计算,然后我需要这个“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);
}

here is the next .cpp

这是下一个 .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);
}

But on compilation It gives me error that "y" was not declared in this scope. Where I've made a mistake?

但是在编译时它给了我一个错误,即未在此范围内声明“y”。我哪里做错了?

Thank you

谢谢

回答by Mike Makuch

On this line

在这条线上

float iGuess = Ing(y);

you don't have y declared, which causes the error. What value do you want to pass to Ing()?

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

You've got 2 parameters defined for Ing(int, float) but only calling it with one.

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

回答by 0x499602D2

y = 0;

should be

应该

int y = 0;

because yhas not yet be declared at this point and you are defining a variable.

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