我刚刚学习c ++(自从我多年前参加一个为期一周的夏令营以来第一天看它)
我正在将我正在使用Java编写的程序转换为C++:
#ifndef ADD_H
#define ADD_H
#define _USE_MATH_DEFINES
#include <iostream>
#include <math.h>
using namespace std;
class Evaluatable {
public:
virtual double evaluate(double x);
};
class SkewNormalEvalutatable : Evaluatable{
public:
SkewNormalEvalutatable();
double evaluate(double x){
return 1 / sqrt(2 * M_PI) * pow(2.71828182845904523536, -x * x / 2);
}
};
SkewNormalEvalutatable::SkewNormalEvalutatable()
{
}
double getSkewNormal(double skewValue, double x)
{
SkewNormalEvalutatable e ();
return 2 / sqrt(2 * M_PI) * pow(2.71828182845904523536, -x * x / 2) * integrate(-1000, skewValue * x, 10000, e);
}
// double normalDist(double x){
// return 1 / Math.sqrt(2 * Math.PI) * Math.pow(Math.E, -x * x / 2);
// }
double integrate (double start, double stop,
int numSteps,
Evaluatable evalObj)
{
double stepSize = (stop - start) / (double)numSteps;
start = start + stepSize / 2.0;
return (stepSize * sum(start, stop, stepSize, evalObj));
}
double sum (double start, double stop,
double stepSize,
Evaluatable evalObj)
{
double sum = 0.0, current = start;
while (current <= stop) {
sum += evalObj.evaluate(current);
current += stepSize;
}
return(sum);
}
// int main()
// {
// cout << getSkewNormal(10.0, 0) << endl;
// return 0;
// }
#endif
Run Code Online (Sandbox Code Playgroud)
错误是:
SkewNormal.h: In function 'double getSkewNormal(double, double)' :
SkewNormal.h: 29: error: 'integrate' was not declared in this scope
SkewNormal.h: In function 'double integrate(double, double, int, Evaluatable)':
SkewNormal.h:41: error: 'sum' was not declared in this scope
Run Code Online (Sandbox Code Playgroud)
积分和求和都应该是函数
这是Java代码,或多或少相同:
public static double negativelySkewed(double skew, int min, int max){
return randomSkew(skew) * (max - min) + min;
}
public static double randomSkew(final double skew){
final double xVal = Math.random();
return 2 * normalDist(xVal) * Integral.integrate(-500, skew * xVal, 100000, new Evaluatable() {
@Override
public double evaluate(double value) {
return normalDist(value);
}
});
}
public static double normalDist(double x){
return 1 / Math.sqrt(2 * Math.PI) * Math.pow(Math.E, -x * x / 2);
}
/** A class to calculate summations and numeric integrals. The
* integral is calculated according to the midpoint rule.
*
* Taken from Core Web Programming from
* Prentice Hall and Sun Microsystems Press,
* http://www.corewebprogramming.com/.
* © 2001 Marty Hall and Larry Brown;
* may be freely used or adapted.
*/
public static class Integral {
/** Returns the sum of f(x) from x=start to x=stop, where the
* function f is defined by the evaluate method of the
* Evaluatable object.
*/
public static double sum(double start, double stop,
double stepSize,
Evaluatable evalObj) {
double sum = 0.0, current = start;
while (current <= stop) {
sum += evalObj.evaluate(current);
current += stepSize;
}
return(sum);
}
/** Returns an approximation of the integral of f(x) from
* start to stop, using the midpoint rule. The function f is
* defined by the evaluate method of the Evaluatable object.
*/
public static double integrate(double start, double stop,
int numSteps,
Evaluatable evalObj) {
double stepSize = (stop - start) / (double)numSteps;
start = start + stepSize / 2.0;
return(stepSize * sum(start, stop, stepSize, evalObj));
}
}
/** An interface for evaluating functions y = f(x) at a specific
* value. Both x and y are double-precision floating-point
* numbers.
*
* Taken from Core Web Programming from
* Prentice Hall and Sun Microsystems Press,
* http://www.corewebprogramming.com/.
* © 2001 Marty Hall and Larry Brown;
* may be freely used or adapted.
*/
public static interface Evaluatable {
public double evaluate(double value);
}
Run Code Online (Sandbox Code Playgroud)
我确定这很简单
另外,我该如何打电话
getSkewNormal(double skewValue, double x)
Run Code Online (Sandbox Code Playgroud)
从SkewNormal.h以外的文件?
AnT*_*AnT 50
在C++中,您应该在使用它们之前声明函数.在您的代码integrate中,在第一次调用之前未声明integrate.这同样适用于sum.因此错误.重新排序定义,以便函数定义在第一次调用该函数之前,或者为每个函数引入[forward]非定义声明.
另外,在C++中禁止使用头文件中定义外部非内联函数.你的定义SkewNormalEvalutatable::SkewNormalEvalutatable,getSkewNormal,integrate等有没有企业在头文件之中.
SkewNormalEvalutatable e();C++中的声明也声明了一个函数e,而不是e你想象的对象.simple SkewNormalEvalutatable e;将声明一个由默认构造函数初始化的对象.
此外,收到的最后一个参数integrate(和sum)由值作为对象Evaluatable类型.这意味着尝试传递SkewNormalEvalutatable作为最后一个参数integrate将导致SkewNormalEvalutatable切片Evaluatable.因此,多态性不起作用.如果您需要多态行为,则必须通过引用或指针接收此参数,而不是按值接收.
在C++中,您的源文件通常在一次传递中从上到下进行解析,因此必须先声明任何变量或函数,然后才能使用它们.这有一些例外,比如在类定义中定义函数内联时,但是代码不是这种情况.
移动integrate上面的定义getSkewNormal,或者在上面添加前向声明getSkewNormal:
double integrate (double start, double stop, int numSteps, Evaluatable evalObj);
Run Code Online (Sandbox Code Playgroud)
这同样适用sum.
通常,在C++函数中必须在调用之前声明它们.所以在定义之前的某个时候getSkewNormal(),编译器需要看到声明:
double integrate (double start, double stop, int numSteps, Evaluatable evalObj);
Run Code Online (Sandbox Code Playgroud)
人们所做的大部分是将所有声明(仅)放在头文件中,并将实际代码(函数和方法的定义)放入单独的源(*.cc或*.cpp)文件中.这巧妙地解决了需要声明所有函数的问题.