'foo' 未在此范围内声明 c++
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6283168/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
'foo' was not declared in this scope c++
提问by Ryan Amos
I'm just learning c++ (first day looking at it since I took a 1 week summer camp years ago)
我只是在学习 C++(自从我多年前参加了 1 周的夏令营以来第一天看它)
I was converting a program I'm working on in Java to 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
The errors were:
错误是:
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
Integrate and sum are both supposed to be functions
Integrate 和 sum 都应该是函数
Here is the Java code, more or less the same:
这是 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);
}
I'm certain it's something very simple
我确定这是非常简单的事情
Also, how do I call
还有,我怎么打电话
getSkewNormal(double skewValue, double x)
From a file outside SkewNormal.h?
来自 SkewNormal.h 之外的文件?
回答by AnT
In C++ you are supposed to declare functions before you can use them. In your code integrate
is not declared before the point of the first call to integrate
. The same applies to sum
. Hence the error. Either reorder your definitions so that function definition precedes the first call to that function, or introduce a [forward] non-defining declaration for each function.
在 C++ 中,您应该先声明函数,然后才能使用它们。在您的代码integrate
中,在第一次调用integrate
. 这同样适用于sum
. 因此错误。要么重新排序您的定义,以便函数定义在对该函数的第一次调用之前,要么为每个函数引入一个 [forward] 非定义声明。
Additionally, defining external non-inline functions in header files in a no-no in C++. Your definitions of SkewNormalEvalutatable::SkewNormalEvalutatable
, getSkewNormal
, integrate
etc. have no business being in header file.
此外,在 C++ 中,在头文件中定义外部非内联函数。你的定义SkewNormalEvalutatable::SkewNormalEvalutatable
,getSkewNormal
,integrate
等有没有企业在头文件之中。
Also SkewNormalEvalutatable e();
declaration in C++ declares a function e
, not an object e
as you seem to assume. The simple SkewNormalEvalutatable e;
will declare an object initialized by default constructor.
SkewNormalEvalutatable e();
C++ 中的声明也声明了一个函数e
,而不是e
您似乎假设的对象。simpleSkewNormalEvalutatable e;
将声明一个由默认构造函数初始化的对象。
Also, you receive the last parameter of integrate
(and of sum
) by valueas an object of Evaluatable
type. That means that attempting to pass SkewNormalEvalutatable
as last argument of integrate
will result in SkewNormalEvalutatable
getting sliced to Evaluatable
. Polymorphism won't work because of that. If you want polymorphic behavior, you have to receive this parameter by reference or by pointer, but not by value.
此外,您通过值作为类型对象接收integrate
(和 of sum
)的最后一个参数。这意味着尝试作为 的最后一个参数传递将导致被切片到. 因此,多态性将不起作用。如果你想要多态行为,你必须通过引用或指针接收这个参数,而不是通过值。Evaluatable
SkewNormalEvalutatable
integrate
SkewNormalEvalutatable
Evaluatable
回答by hammar
In C++, your source files are usually parsed from top to bottom in a single pass, so any variable or function must be declared before they can be used. There are some exceptions to this, like when defining functions inline in a class definition, but that's not the case for your code.
在 C++ 中,您的源文件通常在一次传递中从上到下解析,因此任何变量或函数都必须在使用之前声明。对此有一些例外,例如在类定义中定义内联函数时,但您的代码并非如此。
Either move the definition of integrate
above the one for getSkewNormal
, or add a forward declaration above getSkewNormal
:
将 的定义移到integrate
for 之上getSkewNormal
,或者在上面添加前向声明getSkewNormal
:
double integrate (double start, double stop, int numSteps, Evaluatable evalObj);
The same applies for sum
.
这同样适用于sum
。
回答by Ernest Friedman-Hill
In general, in C++ functions have to be declared before you call them. So sometime before the definition of getSkewNormal()
, the compiler needs to see the declaration:
通常,在 C++ 中,函数必须在调用之前声明。所以在定义之前的某个时候getSkewNormal()
,编译器需要查看声明:
double integrate (double start, double stop, int numSteps, Evaluatable evalObj);
Mostly what people do is put all the declarations (only) in the header file, and put the actual code -- the definitionsof the functions and methods -- into a separate source (*.cc
or *.cpp
) file. This neatly solves the problem of needing all the functions to be declared.
人们所做的主要是将所有声明(仅)放在头文件中,并将实际代码——函数和方法的定义——放入单独的源(*.cc
或*.cpp
)文件中。这巧妙地解决了需要声明所有函数的问题。