I'm trying to compile my first legit program that I'm converting from Java (I ran a test hello world type program to check my compiler and it works). There are three files:
main.cpp
#include <iostream>
#include "skewNormal.h"
using namespace std;
double getSkewNormal(double, double);
int main(int argc, char **argv) {
cout << getSkewNormal(10.0, 0.5) << endl;
}
skewNormal.cpp
#define _USE_MATH_DEFINES
#include <iostream>
#include <math.h>
using namespace std;
#include <skewNormal.h>
double SkewNormalEvalutatable::evaluate(double x)
{
return 1 / sqrt(2 * M_PI) * pow(M_E, -x * x / 2);
}
SkewNormalEvalutatable::SkewNormalEvalutatable()
{
}
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);
}
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 getSkewNormal(double skewValue, double x)
{
SkewNormalEvalutatable e;
return 2 / sqrt(2 * M_PI) * pow(M_E, -x * x / 2) * integrate(-1000, skewValue * x, 10000, e);
}
skewNormal.h
#ifndef SKEWNORMAL_H_INCLUDED
#define SKEWNORMAL_H_INCLUDED
class Evaluatable {
public:
virtual double evaluate(double x);
};
class SkewNormalEvalutatable : Evaluatable{
public:
SkewNormalEvalutatable();
double evaluate(double x);
};
double getSkewNormal(double skewValue, double x);
double integrate (double start, double stop, int numSteps, Evaluatable evalObj);
double sum (double start, double stop, double stepSize, Evaluatable evalObj);
#endif // SKEWNORMAL_H_INCLUDED
Compiling yielded the following error:
main.cpp:9: undefined reference to `getSkewNormal(double, double)'
I'm using Code::Blocks on Ubuntu 10.10.
g++ -o prog main.cpp skewNormal.cpp
, just to be sure. – Ann<>
. In skewNormal.cpp, the including header should be#include "skewNormal.h"
. ( i.e., user defined header included files should be enclosed in" "
). BTW, what you got is not a compilation error but it is a linker error. – Phallus#include "skewNormal.h"
should come beforeusing namespace std;
, in case that using declaration would have an effect on something in the header. – Grig