'foo'没有在这个范围内声明c ++

我只是学习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 

错误是:

 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 

整合和总结都被认为是function

这里是Java代码,或多或less是一样的:

 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/. * &copy; 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/. * &copy; 2001 Marty Hall and Larry Brown; * may be freely used or adapted. */ public static interface Evaluatable { public double evaluate(double value); } 

我确定这很简单

另外,我怎么打电话

 getSkewNormal(double skewValue, double x) 

从SkewNormal.h之外的文件?

在C ++中,你应该声明函数,然后才能使用它们。 在您的代码中, integrate在第一次调用integrate之前未被声明。 sum 。 因此,错误。 要么重新sorting你的定义,以便函数定义在第一次调用该函数之前,或者为每个函数引入一个[forward]非定义声明。

另外,在C ++的no-no中定义头文件中的外部非内联函数。 你的定义SkewNormalEvalutatable::SkewNormalEvalutatablegetSkewNormalintegrate等没有业务在头文件。

另外SkewNormalEvalutatable e(); C ++中的声明声明了一个函数e ,而不是你所假设的一个对象e 。 简单的SkewNormalEvalutatable e; 将声明一个默认构造函数初始化的对象。

此外,您将收到作为Evaluatabletypes的对象的Evaluatable一个按值 integrate (和) 的参数 。 这意味着尝试传递SkewNormalEvalutatable作为integrate最后一个参数将导致SkewNormalEvalutatable被切片为Evaluatable 。 多态性不会因此而起作用。 如果你想要多态行为,你必须通过引用或者指针来接收这个参数,而不是通过值。

在C ++中,你的源代码文件通常是从上到下分析的,所以任何variables或者函数在使用前都必须声明。 有一些例外,比如在类定义中内联定义函数,但是代码不是这样。

要么移动getSkewNormal之上的integrate的定义,要么在getSkewNormal之上添加一个前向声明:

 double integrate (double start, double stop, int numSteps, Evaluatable evalObj); 

sum

一般来说,在C ++中函数必须在调用之前声明。 所以在getSkewNormal()的定义之前,编译器需要看到声明:

 double integrate (double start, double stop, int numSteps, Evaluatable evalObj); 

大多数情况下,人们所做的只是把所有的声明放在头文件中,然后把实际的代码 – 函数和方法的定义 – 放到一个单独的源文件( *.cc*.cpp )中。 这整齐地解决了需要声明所有function的问题。