C++ “不平凡”;未在此范围内声明”;错误
以下函数出现奇怪的“未在此范围内声明”错误:C++ “不平凡”;未在此范围内声明”;错误,c++,function,compiler-errors,scope,C++,Function,Compiler Errors,Scope,以下函数出现奇怪的“未在此范围内声明”错误: double monteCarlo(void) { double intervalArea = 2*(upperBound - lowerBound); // (f_max(x)) - f_min(x))*(upperBound - lowerBound) - Could be calculated from derivative, but known for this function. for (uint currentPoint
double monteCarlo(void)
{
double intervalArea = 2*(upperBound - lowerBound); // (f_max(x)) - f_min(x))*(upperBound - lowerBound) - Could be calculated from derivative, but known for this function.
for (uint currentPoints = (numPoints/100); currentPoints < numPoints; currentPoints += (numPoints/100))
{
double area = randx = randy = 0;
uint underCurve = 0;
gsl_rng* rndGen = gsl_rng_alloc (gsl_rng_mt19937); // Initialize random number generation
gsl_rng_set(rndGen,timeSeed()); // Seed random number generation
for (uint point= 1; point <= currentPoints; point++)
{
randx = gsl_rng_uniform(rndGen); randy = ((2*gsl_rng_uniform_pos(rndGen)) -1);
if (randy <= f(ranx))
{
underCurve++;
}
}
area = (underCurve/currentPoints)*intervalArea;
output << currentSubintervals << "\t" << area << std::endl;
}
return area;
}
double trapezoidal(void)
{
for (uint currentSubintervals = 1; currentSubintervals <= subintervals; currrentSubintervals++)
{
double area = stepSize = sum = 0;
double stepSize = ((upperBound - lowerBound)/currentSubintervals);
double sum = (f(lowerBound) + f(upperBound))/2;
for (uint currentInterval = 1; i < currentSubintervals; currentInterval++)
{
sum += f(lowerBound + (currentInterval*stepSize));
}
area = stepSize*sum;
output << currentSubintervals << "\t" << area << std::endl;
}
return area;
}
这是。。。非常奇怪,因为变量都是在函数本身中声明和使用的。我试着在MWE中复制它,但我什么都没有
我唯一能问的是。。。帮助我不知道这里出了什么问题。我已经试了几个小时,在谷歌上搜索了很多
#include "integrate.hpp"
//=======================
// Globals
static uint algorithm_flag = 0; // Flag for which algoirthm to use
static std::string algorithm = "none";
static double lowerBound = 0, upperBound = 1; // "Global" bounds for algorithms
static uint subintervals = 0, numPoints = pow(2,16);
static int option_index = 0;
static std::ofstream output ("integrate.data");
//=======================
// Main
int main(int argc, char **argv)
{
std::cout << " Numerical Integrator of cos(1/x)!\n"
<< "-----------------------------------\n";
if (!(handleArgs(argc, argv)))
{
throw std::invalid_argument(argv[option_index]);
return -1;
}
std::cout << " Algorithm: " << algorithm << std::endl
<< " Lower Bound: " << lowerBound << std::endl
<< " Upper Bound: " << upperBound << std::endl;
switch(algorithm_flag)
{
case 1:
std::cout << " Number of Points: " << numPoints << std::endl << " Number of Subintervals: " << subintervals;
break;
case 2:
std::cout << " Number of Points: " << numPoints;
break;
case 3:
std::cout << " Number of Subintervals: " << subintervals;
break;
}
std::cout << std::endl << "-----------------------------------" << std::endl;
double area, diff, percentError, actualArea = -0.08441095055957388688903177037359518055393632433151889234592026720612077182783481670736342350213473343;
if (algorithm_flag == 2 || algorithm_flag == 1)
{
std::cout << " Monte Carlo:" << std::endl;
area = monteCarlo();
diff = area - actualArea;
percentError = (diff/actualArea)*100;
std::cout << " Calculated area:\t" << area << std::endl
<< " Error:\t\t\t" << diff << std::endl
<< " Accuracy:\t\t" << percentError << "%" << std::endl;
}
else if (algorithm_flag == 3 || algorithm_flag == 1)
{
std::cout << " Trapezoid: " << std::endl;
area = trapezoidal();
diff = area - actualArea;
percentError = (diff/actualArea)*100;
std::cout << " Calculated area:\t" << area << std::endl
<< " Error:\t\t\t" << diff << std::endl
<< " Accuracy:\t\t" << percentError << "%" << std::endl;
}
else
{
std::cout << " Please specify a numerical integration algorithm!" << std::endl
<< "\tSpecify the -m flag for Monte Carlo" << std::endl
<< "\tSpecify the -t flag for Trapezoial" << std::endl
<< "\tSpecify the -a flag for both algorithms" << std::endl;
throw std::logic_error(algorithm);
return -2;
}
std::cout << std::endl << "-----------------------------------" << std::endl
<< "Please see ./integrate.data for the full details of the integration." << std::endl;
output.close();
return 0;
}
//=======================
// Functions
double f(double x)
{
double y = cos(1/x);
return y;
}
double monteCarlo(void)
{
double intervalArea = 2*(upperBound - lowerBound); // (f_max(x)) - f_min(x))*(upperBound - lowerBound) - Could be calculated from derivative, but known for this function.
for (uint currentPoints = (numPoints/100); currentPoints < numPoints; currentPoints += (numPoints/100))
{
double area = randx = randy = 0;
uint underCurve = 0;
gsl_rng* rndGen = gsl_rng_alloc (gsl_rng_mt19937); // Initialize random number generation
gsl_rng_set(rndGen,timeSeed()); // Seed random number generation
for (uint point= 1; point <= currentPoints; point++)
{
randx = gsl_rng_uniform(rndGen); randy = ((2*gsl_rng_uniform_pos(rndGen)) -1);
if (randy <= f(ranx))
{
underCurve++;
}
}
area = (underCurve/currentPoints)*intervalArea;
output << currentSubintervals << "\t" << area << std::endl;
}
return area;
}
double trapezoidal(void)
{
for (uint currentSubintervals = 1; currentSubintervals <= subintervals; currrentSubintervals++)
{
double area = stepSize = sum = 0;
double stepSize = ((upperBound - lowerBound)/currentSubintervals);
double sum = (f(lowerBound) + f(upperBound))/2;
for (uint currentInterval = 1; i < currentSubintervals; currentInterval++)
{
sum += f(lowerBound + (currentInterval*stepSize));
}
area = stepSize*sum;
output << currentSubintervals << "\t" << area << std::endl;
}
return area;
}
bool handleArgs(int argc, char *argv[])
{
int arg = 0;
bool rtVal = true;
while ((arg = getopt_long (argc, argv, "mtau:l:i:p:",long_options, &option_index)) != -1)
{
if(optarg == 0)
{
continue;
}
switch(arg)
{
case 'a':
if (algorithm_flag > 1)
{
std::cout << "Cannot specify more than one algoirthm type, please use --all for both, or use only one";
}
algorithm_flag = 1;
algorithm = "Monte Carlo and Trapezoidal";
break;
case 'm':
if (algorithm_flag)
{
std::cout << "Cannot specify more than one algoirthm type, please use --all for both, or use only one";
}
algorithm_flag = 2;
algorithm = "Monte Carlo";
break;
case 't':
if (algorithm_flag)
{
std::cout << "Cannot specify more than one algoirthm type, please use --all for both, or use only one";
}
algorithm_flag = 3;
algorithm = "Trapezoidal";
break;
case 'u':
upperBound = atoi(optarg);
break;
case 'l':
lowerBound = atoi(optarg);
break;
case 'i':
subintervals = atoi(optarg);
break;
case 'p':
numPoints = atoi(optarg);
break;
case '?':
/* getopt already printed an error message. */
rtVal = false;
break;
default:
rtVal = false;
}
if(!(rtVal))
{
std::cout << "Invalid option " << arg;
if (optarg)
{
std::cout << " with arg " << optarg;
}
std::cout << std::endl;
throw std::invalid_argument(argv[option_index]);
break;
}
}
return rtVal;
}
#包括“integrate.hpp”
//=======================
//全球的
静态uint算法_标志=0;//要使用哪个算法的标志
静态std::string algorithm=“none”;
静态双下限=0,上限=1;//算法的“全局”界
静态uint子区间=0,numPoints=pow(2,16);
静态int选项_索引=0;
流输出的静态std::(“integrate.data”);
//=======================
//主要
int main(int argc,字符**argv)
{
std::coutdouble area=stepSize=sum=0;
仅是area
的声明,并假设已声明stepSize
和sum
。它们不是。
换成
double area, stepSize, sum;
area = stepSize = sum = 0;
也
及
这是两件不同的事。对此,我只能说——兄弟
double area = randx = randy = 0;
可以假定,您希望声明三个变量。它没有;它只是声明区域
,并使用randx=randy=0
作为初始值设定项。您需要三个单独的声明项:
double area=0, randx=0, randy=0;
或三个单独的声明声明
double area = randx = randy = 0;
这是什么意思?它说“定义一个double类型的变量,命名为area
,并用已经存在的变量randx
的值初始化它,该变量将被分配另一个已经存在的变量randy
,该变量将被分配值0。”
编译器不知道任何名为randx
和randy
的现有变量
我想你真正想要的是定义三个新变量,所有变量都用0初始化。你可以在一行中这样做:
double area=0, randx=0, randy=0;
或者一个接一个地定义它们:
double area=0;
double randx = 0;
double randy = 0;
这可能需要更多的输入,但可以提高可读性,而且一个程序更容易被人读而不是被人写,所以节省一些按键是不值得的
更好的做法是在需要变量时定义变量,而不是更早
然后还有第二个错误:返回一个区域
——但是您只在for循环中定义了该变量,因此在调用返回时该变量不存在
double monteCarlo(void)
{
const double intervalArea = 2*(upperBound - lowerBound);
for (uint currentPoints = (numPoints/100); currentPoints < numPoints; currentPoints += (numPoints/100))
{
gsl_rng* rndGen = gsl_rng_alloc (gsl_rng_mt19937); // Initialize random number generation
gsl_rng_set(rndGen,timeSeed()); // Seed random number generation
uint underCurve = 0;
for (uint point= 1; point <= currentPoints; point++)
{
double randx = gsl_rng_uniform(rndGen);
double randy = ((2*gsl_rng_uniform_pos(rndGen)) -1);
if (randy <= f(randx))
{
underCurve++;
}
}
double area = (underCurve/currentPoints)*intervalArea;
output << currentSubintervals << "\t" << area << std::endl;
}
return /** ??? **/;
}
double monteCarlo(无效)
{
常数双区间面积=2*(上界-下界);
对于(uint currentPoints=(numPoints/100);currentPoints 对于(uint point=1;point+1.dto对于randx
和randy
,当然。而ranx
似乎也是一个打字错误。Lol@cu rrr entsubinterval没有声明它是正确的,兄弟
double area = randx = randy = 0;
double area=0, randx=0, randy=0;
double area=0;
double randx = 0;
double randy = 0;
double monteCarlo(void)
{
const double intervalArea = 2*(upperBound - lowerBound);
for (uint currentPoints = (numPoints/100); currentPoints < numPoints; currentPoints += (numPoints/100))
{
gsl_rng* rndGen = gsl_rng_alloc (gsl_rng_mt19937); // Initialize random number generation
gsl_rng_set(rndGen,timeSeed()); // Seed random number generation
uint underCurve = 0;
for (uint point= 1; point <= currentPoints; point++)
{
double randx = gsl_rng_uniform(rndGen);
double randy = ((2*gsl_rng_uniform_pos(rndGen)) -1);
if (randy <= f(randx))
{
underCurve++;
}
}
double area = (underCurve/currentPoints)*intervalArea;
output << currentSubintervals << "\t" << area << std::endl;
}
return /** ??? **/;
}