'foo'没有在这个范围c ++中声明 [英] 'foo' was not declared in this scope c++

查看:138
本文介绍了'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);
//}

双积分(双开始,双停止,
int numSteps,
可计算evalObj)
{
double stepSize = (停止 - 开始)/(双)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;
//返回0;
//}
#endif

错误是:

  SkewNormal.h:在函数'double getSkewNormal(double,double)':
SkewNormal.h:29:error:'integrate '未在此范围内声明
SkewNormal.h:在函数'double integrate(double,double,int,Evaluatable)':
SkewNormal.h:41:error:'sum' this scope

集成和求和都应该是函数



这里是Java代码,或多或少相同:

  public static double negativeSkewed 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 ){
return normalDist(value);
}
});
}

public static double normalDist(double x){
return 1 / Math.sqrt(2 * Math.PI)* Math.pow(Math.E,-x * x / 2);
}

/ **用于计算求和和数值积分的类。
*积分根据中点规则计算。
*
*从核心Web编程中获取
* Prentice Hall和Sun Microsystems Press,
* http://www.corewebprogramming.com/。
*& copy; 2001 Marty Hall和Larry Brown;
*可以自由使用或改编。
* /

public static class Integral {
/ **返回从x = start到x = stop的f(x)的总和,其中
*函数f由
* Evaluatable对象的evaluate方法定义。
* /

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);
}

/ **使用中点规则,返回从
*开始到停止的f(x)的积分的近似值。函数f是由Evaluatable对象的evaluate方法定义的
*。
* /

public static double integrate(double start,double stop,
int numSteps,
Evaluatable evalObj){
double stepSize =(stop - 开始)/(双)numSteps;
start = start + stepSize / 2.0;
return(stepSize * sum(start,stop,stepSize,evalObj));
}
}

/ **用于在特定
*值处评估函数y = f(x)的接口。 x和y都是双精度浮点
*数字。
*
*从核心Web编程中获取
* Prentice Hall和Sun Microsystems Press,
* http://www.corewebprogramming.com/。
*& copy; 2001 Marty Hall和Larry Brown;
*可以自由使用或改编。
* /
public static interface可评估的{
public double evaluate(double value);
}

我确定这很简单



此外,我如何调用

  getSkewNormal(double skewValue,double x)



来自SkewNormal.h之外的文件?

解决方案

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



- 在C ++中的无头的文件中的inline函数。您的定义 SkewNormalEvalutatable :: SkewNormalEvalutatable getSkewNormal integrate



还有 SkewNormalEvalutatable e(); 在C ++中的声明声明一个函数 e ,而不是您似乎假设的对象 e 。简单的 SkewNormalEvalutatable e; 会声明一个由默认构造函数初始化的对象。



整合(以及 sum 作为 Evaluatable 类型。这意味着尝试传递 SkewNormalEvalutatable 作为 integrate 的最后一个参数将导致 SkewNormalEvalutatable 获取切片为 Evaluatable 。多态性不会工作,因为。如果你想要多态行为,你必须通过引用或者通过指针接收这个参数,而不是通过值。


I'm just learning c++ (first day looking at it since I took a 1 week summer camp years ago)

I was converting a program I'm working on in Java to 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

Here is the Java code, more or less the same:

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);
}

I'm certain it's something very simple

Also, how do I call

getSkewNormal(double skewValue, double x)

From a file outside SkewNormal.h?

解决方案

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.

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.

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.

Also, you receive the last parameter of integrate (and of sum) by value as 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.

这篇关于'foo'没有在这个范围c ++中声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆