- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我收到以下功能的怪异的“未在此范围内声明”错误:
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;
}
g++ -c -Wall -std=c++11 integrate.cpp -o integrate.o
integrate.cpp: In function ‘double monteCarlo()’:
integrate.cpp:118:17: error: ‘randx’ was not declared in this scope
integrate.cpp:118:25: error: ‘randy’ was not declared in this scope
integrate.cpp:126:19: error: ‘ranx’ was not declared in this scope
integrate.cpp:133:13: error: ‘currentSubintervals’ was not declared in this scope
integrate.cpp:136:9: error: ‘area’ was not declared in this scope
integrate.cpp: In function ‘double trapezoidal()’:
integrate.cpp:141:74: error: ‘currrentSubintervals’ was not declared in this scope
integrate.cpp:143:17: error: ‘stepSize’ was not declared in this scope
integrate.cpp:143:28: error: ‘sum’ was not declared in this scope
integrate.cpp:148:34: error: ‘i’ was not declared in this scope
integrate.cpp:156:9: error: ‘area’ was not declared in this scope
integrate.cpp:157:1: warning: control reaches end of non-void function [-Wreturn-type]
integrate.cpp: In function ‘double monteCarlo()’:
integrate.cpp:137:1: warning: control reaches end of non-void function [-Wreturn-type]
make: *** [integrate.o] Error 1
#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;
}
//=======================
// Guard Statment
#ifndef __SAWHPP_INCLUDED__
#define __SAWHPP_INCLUDED__
//=======================
// Dependencies
#include <iostream>
#include <vector>
#include <string>
#include <cmath> // Math Functions Used
#include <ctime> // Random Number Seed
#include <stdexcept>
#include <fstream>
#include <cstdlib>
#include <getopt.h>
#include <gsl/gsl_rng.h> // Random Number Generation
//=======================
// Prototypes
bool handleArgs(int argc, char *argv[]);
double monteCarlo(void);
double trapezoidal(void);
double f(double x);
//=======================
// Objects and Datatypes
typedef unsigned long int ulong;
typedef unsigned int uint;
static struct option long_options[] =
{
{"montecarlo", no_argument, 0, 'm'},
{"trapezoidal", no_argument, 0, 't'},
{"all" , no_argument, 0, 'a'},
{"upper", optional_argument, 0, 'u'},
{"lower", optional_argument, 0, 'l'},
{"subintervals", optional_argument, 0, 'i'},
{"points", optional_argument, 0, 'p'},
{nullptr, 0, 0, 0}
};
//=======================
// Trivial Functions
unsigned long int timeSeed()
{
std::time_t rawtime;
std::tm* currentTime;
std::string timeString;
std::time(&rawtime); // Seconds since 00:00:00 UTC, January 1, 1970
currentTime = std::gmtime(&rawtime); // Convert to GMT
std::strftime(&timeString[0],timeString.max_size(),"%Y%m%d%H%M%S",currentTime); // Convert to String
return std::strtol(timeString.c_str(),nullptr,10); // Convert to int.
}
#endif // Guard Statment
最佳答案
double area = stepSize = sum = 0;
仅是area
的声明,并假设stepSize
和sum
已经声明。他们不是。
更改为
double area, stepSize, sum;
area = stepSize = sum = 0;
currrentSubintervals
currentSubintervals
关于c++ - 琐碎的 “Not declared in this scope”错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20100210/
我已经使用 vue-cli 两个星期了,直到今天一切正常。我在本地建立这个项目。 https://drive.google.com/open?id=0BwGw1zyyKjW7S3RYWXRaX24tQ
您好,我正在尝试使用 python 库 pytesseract 从图像中提取文本。请找到代码: from PIL import Image from pytesseract import image_
我的错误 /usr/bin/ld: errno: TLS definition in /lib/libc.so.6 section .tbss mismatches non-TLS reference
我已经训练了一个模型,我正在尝试使用 predict函数但它返回以下错误。 Error in contrasts<-(*tmp*, value = contr.funs[1 + isOF[nn]])
根据Microsoft DataConnectors的信息我想通过 this ODBC driver 创建一个从 PowerBi 到 PostgreSQL 的连接器使用直接查询。我重用了 Micros
我已经为 SoundManagement 创建了一个包,其中有一个扩展 MediaPlayer 的类。我希望全局控制这个变量。这是我的代码: package soundmanagement; impo
我在Heroku上部署了一个应用程序。我正在使用免费服务。 我经常收到以下错误消息。 PG::Error: ERROR: out of memory 如果刷新浏览器,就可以了。但是随后,它又随机发生
我正在运行 LAMP 服务器,这个 .htaccess 给我一个 500 错误。其作用是过滤关键字并重定向到相应的域名。 Options +FollowSymLinks RewriteEngine
我有两个驱动器 A 和 B。使用 python 脚本,我在“A”驱动器中创建一些文件,并运行 powerscript,该脚本以 1 秒的间隔将驱动器 A 中的所有文件复制到驱动器 B。 我在 powe
下面的函数一直返回这个错误信息。我认为可能是 double_precision 字段类型导致了这种情况,我尝试使用 CAST,但要么不是这样,要么我没有做对...帮助? 这是错误: ERROR: i
这个问题已经有答案了: Syntax error due to using a reserved word as a table or column name in MySQL (1 个回答) 已关闭
我的数据库有这个小问题。 我创建了一个表“articoli”,其中包含商品的品牌、型号和价格。 每篇文章都由一个 id (ID_ARTICOLO)` 定义,它是一个自动递增字段。 好吧,现在当我尝试插
我是新来的。我目前正在 DeVry 在线学习中级 C++ 编程。我们正在使用 C++ Primer Plus 这本书,到目前为止我一直做得很好。我的老师最近向我们扔了一个曲线球。我目前的任务是这样的:
这个问题在这里已经有了答案: What is an undefined reference/unresolved external symbol error and how do I fix it?
我的网站中有一段代码有问题;此错误仅发生在 Internet Explorer 7 中。 我没有在这里发布我所有的 HTML/CSS 标记,而是发布了网站的一个版本 here . 如您所见,我在列中有
如果尝试在 USB 设备上构建 node.js 应用程序时在我的树莓派上使用 npm 时遇到一些问题。 package.json 看起来像这样: { "name" : "node-todo",
在 Python 中,您有 None单例,在某些情况下表现得很奇怪: >>> a = None >>> type(a) >>> isinstance(a,None) Traceback (most
这是我的 build.gradle (Module:app) 文件: apply plugin: 'com.android.application' android { compileSdkV
我是 android 的新手,我的项目刚才编译和运行正常,但在我尝试实现抽屉导航后,它给了我这个错误 FAILURE: Build failed with an exception. What wen
谁能解释一下?我想我正在做一些非常愚蠢的事情,并且急切地等待着启蒙。 我得到这个输出: phpversion() == 7.2.25-1+0~20191128.32+debian8~1.gbp108
我是一名优秀的程序员,十分优秀!