41

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?

Mat
  • 202,337
  • 40
  • 393
  • 406
Ryan Amos
  • 5,422
  • 4
  • 36
  • 56
  • You need to declare a function before you can call it in C++. Also, prefer `M_E` to `2.71828...` – Nemo Jun 08 '11 at 18:04

3 Answers3

58

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.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
  • 1
    You can probably also mention that `SkewNormalEvalutatable e ();` is wrong and won't create any objects. – Naveen Jun 08 '11 at 18:07
  • Awesome :D Thank you! I put it in a header file because I wasn't sure how to use multiple files and header seemed more straightforward. Should I have another file named "SkewNormal.cpp" and define the functiosn there? – Ryan Amos Jun 08 '11 at 18:12
  • @Somanayr: That's how it is normally done. You put non-defining *declarations* of your functions into header file. The *definitions* go into .cpp file. – AnT stands with Russia Jun 08 '11 at 18:15
  • Mind if I ask another question? My main.cpp file (http://pastie.org/private/oifaxdkebrsi1oqofn0ea) is giving me an error in compilation: main.cpp:7:undefined reference to 'getSkewNormal(double,double)'. Could you tell me what this is about/how to fix it? I moved the functions into SkewNormal.cpp (http://pastie.org/private/4n8cys2zpr2bfw3brucsg) and just declarations in SkewNormal.h (http://pastie.org/private/1g7c66xjfd3qu6idfxmwa) – Ryan Amos Jun 08 '11 at 19:17
  • @Somanayr: Now your program consists of two source files - `main.cpp` and `SkewNormal.cpp`. You have to compile both files and link the results (objects files) together. You apparently, are either not compiling `SkewNormal.cpp` at all or not linking the result of its compilation into the final program. What compiler are you using? – AnT stands with Russia Jun 08 '11 at 19:23
  • @AndreyT I'm using KDevelop. I'm using Ubuntu 10.10. I also have MonoDevelop, Geany, and Code::Blocks installed. – Ryan Amos Jun 08 '11 at 19:28
  • @AndreyT I switched to Code::Blocks and it's giving the same error. – Ryan Amos Jun 08 '11 at 19:58
  • @Somanayr: Well, again, you have top add `SkewNormal.cpp` to your "project", "makefile" or whatever it is called in KDevelop. – AnT stands with Russia Jun 08 '11 at 20:16
  • @AndreyT I did that and it still doesn't seem to be working :/ Here's the file: http://pastebin.com/dQ0Kbrna. When I compile, it tells me that SkewNormal.h doesn't exist. Thanks for all the help so far! – Ryan Amos Jun 08 '11 at 20:20
12

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.

Either move the definition of integrate above the one for getSkewNormal, or add a forward declaration above getSkewNormal:

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

The same applies for sum.

hammar
  • 138,522
  • 17
  • 304
  • 385
6

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:

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 definitions of 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.

Ernest Friedman-Hill
  • 80,601
  • 10
  • 150
  • 186