尝试在c ++

时间:2019-05-22 10:06:50

标签: c++ fortran

我对C ++的了解非常有限,甚至对fortran的了解也很少,我目前正在尝试从c ++ main调用fortran子例程。通过一些示例,我能够提出以下代码来调用

subroutine fireballess(ear,ne,parames,ifl,photar,photer)

这是我的C ++代码:

#include <stdlib.h>
#include <stdio.h>
#include <iostream>

using namespace std;

extern "C" void fireballess_( double *fear, int fne,double* fparames, int fifl, double *fphotar, double *fphoter);

int main(int argc, char ** argv)
{
    int ne,ifl;
    double *ear;
    double *parames;

    double *photar;
    double *photer;


    parames = new double[9];

// parames=[4.3,0.23,0.5,0.5,1.5,1.,1000.,2.15,3.]
        parames[0]=4.3;
        parames[1]=0.23;
        parames[2]=0.5;
        parames[3]=0.5;
        parames[4]=1.5;
        parames[5]=1.;
        parames[6]=1000.;
        parames[7]=2.15;
        parames[8]=3.;

ne = 2;

    ear = new double[ne];
 ear[0] = 0.;
 ear[1] = 20.;
 ear[2] = 40.;
 ifl=2;

    photar = new double[ne];
    photer = new double[ne];

    // Call a Fortran subroutine
    //subroutine_sum_(&size,vec,&sum);
    fireballess_(&ear,ne,&parames,ifl,&photar,&photer);

    cout << "Calling a Fortran subroutine" << endl;
    cout << "===============================" << endl;

for (int i=0;i<=ne;i++){
    cout << "ear = " <<ear[i-1]<< " - "<<ear[i] << endl;
    cout << "photar = " << photar[i] << endl;
    cout << "photer = " << photer[i] << endl << endl;
}

    delete[] ear;
    delete[] parames;
    delete[] photar;
    delete[] photer;
}

但是,当我尝试编译时,出现以下错误:

call_fortran.cpp: In function ‘int main(int, char**)’:
call_fortran.cpp:45:53: error: cannot convert ‘double**’ to ‘double*’ for argument ‘1’ to ‘void fireballess_(double*, int, double*, int, double*, double)’
     fireballess_(&ear,ne,&parames,ifl,&photar,photer);
我不理解的

,因为photer变量遵循photar变量的相同路径,所以不会产生任何错误。 我希望比我更了解指针的人可以在这里为我提供帮助。 谢谢

1 个答案:

答案 0 :(得分:1)

ear的类型为double*,因此&ear的类型为double**,对于fortran函数原型来说,这是不合适的。摆脱&可能可以解决问题(&parames,&photar和&photer也是双重**):

更改

fireballess_(&ear,ne,&parames,ifl,&photar,&photer);

fireballess_(ear,ne,parames,ifl,photar,photer);

fortran函数原型似乎是void fireballess_(double*, int, double*, int, double*, double)。如果确实如此,则photer仍必须更改为photer[i],其中(i是一个数组索引,如0、1、2 ...)。

如果您真的想了解发生了什么,我建议阅读一本不错的C ++指针教程。