Reputation: 99
I have very limited knowledge of C++ and even less of fortran, I am currently trying to call a fortran subroutine from a c++ main. Following some examples I was able to come up with the following code to call the
subroutine fireballess(ear,ne,parames,ifl,photar,photer)
here is my C++ code:
#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,¶mes,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;
}
however, when I try to compile I get the following error:
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,¶mes,ifl,&photar,photer);
which I do not understand, since the photer variable follows the very same path of the photar variable, that gives no errors instead. I hope someone with a better understanding of pointers than me can help me here. Thanks
Upvotes: 4
Views: 3943
Reputation: 2117
ear
is of type double*
so that &ear
is of type double**
, which is not ok for the fortran function prototype. Getting rid of the &
s might solve the issue (¶mes, &photar and &photer are also double**):
Change
fireballess_(&ear,ne,¶mes,ifl,&photar,&photer);
to
fireballess_(ear,ne,parames,ifl,photar,photer);
The fortran function prototype seems to be void fireballess_(double*, int, double*, int, double*, double)
. If that is really so, then photer
still has to be changed to photer[i]
where (i is an array-index like 0, 1, 2...).
If you really want to understand what is going on, I suggest reading a good C++ Pointer Tutorial.
Upvotes: 1