0

Okay so I am relatively new to c++ and I am trying to figure out how to use function pointers. I have a function which is a simple numerical integration and I am trying to pass to it which function to integrate and what the limits of integration are. I am doing this in Xcode and the error is in the main code saying "no matching function to call SimpsonIntegration". If someone could please help I would appreciate it. Also since I am learning, any other criticism will be appreciated as well. The main.cpp function is below.

#include <iostream>
#include "simpson7.h"
#include <cmath>

using namespace std;



int main(int argc, const char * argv[])
{
    double a=0;
    double b=3.141519;
    int bin = 1000;

    double (*sine)(double);
    sine= &sinx;



    double n = SimpsonIntegration(sine, 1000, 0, 3.141519);

      cout << sine(0)<<"  "<<n;
}

The simpson.h file is below:

#ifndef ____1__File__
#define ____1__File__

#include <iostream>
template <typename mytype>

 double SimpsonIntegration(double (*functocall)(double) ,int bin, double a, double b);
 extern double (*functocall)(double);
 double sinx(double x);

#endif /* defined(____1__File__) */

The simpson.cpp file is next:

#include "simpson7.h"
#include <cmath>
#include <cassert>


//The function will only run if the number of bins is a positive integer.



double sinx(double x){

    return sin(x);
}

double SimpsonIntegration( double (*functocall)(double),  int bin, double a, double b){

    assert(bin>0);
    //assert(bin>>check);

    double integralpart1=(*functocall)(a), integralpart2=(*functocall)(b);
    double h=(b-a)/bin;
    double j;

    double fa=sin(a);
    double fb=sin(b);

    for (j=1; j<(bin/2-1); j++) {
        integralpart1=integralpart1+(*functocall)(a+2*j*h);
    }

    for (double l=1; l<(bin/2); l++) {
        integralpart2=integralpart2+(*functocall)(a+(2*l-1)*h);
    }

    double totalintegral=(h/3)*(fa+2*integralpart1+4*integralpart2 +fb);



    return totalintegral;

}

Well okay now that I fixed that silly error I tried to compile and I got this error: "Linker command failed with exit code 1".

taylor
  • 1
  • 3
  • Also just to clarify the other two files are simpson7.h and simpson7.cpp so that is not the cause – taylor Nov 04 '13 at 06:20

1 Answers1

4

If you look at the header file, you have this declaration

template <typename mytype>
double SimpsonIntegration(double (*functocall)(double) ,int bin, double a, double b);

And in the source file you have

double SimpsonIntegration( double (*functocall)(double),  int bin, double a, double b)

That is not the same function. The compiler tries to search for the non-template function, but that hasn't been declared and so it gives an error.

The simple solution is to remove the template specification in the header file.

If you do want the function to be a template function, then you should be careful with the separation of the declaration and definition, see e.g. this old question.

Community
  • 1
  • 1
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Thank you so much it worked! Such a simple thing! Thank you for helping this beginner! – taylor Nov 04 '13 at 14:40
  • I ran into another error that said "Linker command failed with exit code 1" – taylor Nov 04 '13 at 20:43
  • @taylor That may be another problem, and as such may warrant a new question. What you should do is to post the *complete* and *unedited* error log in the question, and only post the relevant lines (i.e. either the lines indicated by the error messages, or the the usage of the symbols mentioned in the error messages). – Some programmer dude Nov 05 '13 at 05:11