我在這個項目中遇到了很多問題,我想了很多。但是這一次,我不能'y'未在此範圍內聲明,
這裏我有一些計算,然後我需要這個「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」沒有在此範圍內聲明。 我犯了什麼錯誤?
謝謝
將其更改爲'int y = 0;' –
您需要了解C++ Scopes以瞭解錯誤消息在告訴您如何解決問題時不能更清楚:http://www.tutorialspoint。 com/cplusplus/cpp_variable_scope.htm –
如果我將其更改爲int y = 0,它將會影響參數 –