简体   繁体   English

无法将函数指针传递到头文件中声明的函数

[英]Cannot pass function pointer to function declared in header file

Okay so I am relatively new to c++ and I am trying to figure out how to use function pointers. 好的,所以我对C ++还是比较陌生,我正在尝试弄清楚如何使用函数指针。 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". 我正在Xcode中执行此操作,并且错误在主代码中显示“没有匹配函数可以调用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. main.cpp函数如下。

#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: simpson.h文件如下:

#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: 接下来是simpson.cpp文件:

#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". 好吧,现在我修复了我尝试编译的那个愚蠢的错误,并得到了这个错误:“链接器命令失败,退出代码为1”。

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 eg this old question . 如果您确实希望该函数成为模板函数,那么在声明和定义的分离上应格外小心,例如,请参见此旧问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM