Applying callback to call gsl_integration

I am a newbie with c++ and I am trying to modify a code and use gsl_integration library of c in a class called Cosmology. In order to assign member functions to to form a pointer for gsl, I used callback procedure I found here and applying it. But I got a lot of error massages

Cosmology.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <cmath>
#include <gsl/gsl_integration.h>

using namespace std;
struct CCallbackHolder
{
  Cosmology* cls;
  void* data;
};

class Cosmology {
private:
    static constexpr double c = 299792458.0, Mpc2Km = 3.08567758e+19, Yrs2Sec = 3.15569e7;
    double H0 = 67.77, OmegaM = (0.022161+0.11889)/(H0*H0), OmegaL = 0.6914, OmegaG = 8.24e-5, OmegaK = 0.0009;
    double Ez(double z);
    double Hz(double z, void* params);
    static double CCallback(double z,void* param)
    {
      CCallbackHolder* h = static_cast<CCallbackHolder*>(param);
      return h->cls->Hz(h->data);
    }
public:
    double distH, timeH;
    Cosmology() = default;
    Cosmology(double );
    Cosmology(double , double);
    Cosmology(double , double , double);
    Cosmology(double, double , double, double);
    Cosmology(double , double , double , double , double );
    double distC(double z);
  } cosmo;

Cosmology.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <cmath>
#include <gsl/gsl_integration.h>
#include "Cosmology.h"
#include<iostream>
void Cosmology::Cosmology(double h0, double omegaM, double omegaL, double omegaG, double omegaK){
     H0=h0;
     OmegaM=omegaM;
     OmegaL=omegaL; 
     OmegaG=omegaG;
     OmegaK=omegaK;
}

double Cosmology::Hz(double z, void* params)  {
    double result = 1.0/pow(OmegaL + pow(1.0+z,3.0)*OmegaM + pow(1.0+z,4.0)*OmegaG + pow(1.0+z,2.0)*OmegaK, 0.5);
    return result;
    }

double Cosmology::distC(double z) { 
    double lower_limit = 0.0, abs_error = 1.0e-8, rel_error = 1.0e-8, alpha = 0.0, result, error;
    gsl_integration_workspace *work_ptr = gsl_integration_workspace_alloc(1000);
    gsl_function Hz_function;
    void* params_ptr = &alpha;
    Hz_function.function = &Cosmology::CCallback;
    Hz_function.params = params_ptr;
    gsl_integration_qags(&Hz_function, lower_limit, z, abs_error, rel_error, 1000, work_ptr, &result, &error);
    return distH*result;
    }
using namespace std;    
int main () {
  Cosmology cosmo;
  cout << "Comoving Distance: " << cosmo.distC (0.3);
  return 0;
}


errors are as following:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Cosmology.h:10: error: ISO C++ forbids declaration of ‘Cosmology’ with no type
Cosmology.h:10: error: expected ‘;’ before ‘*’ token
Cosmology.h:16: error: ISO C++ forbids declaration of ‘constexpr’ with no type
Cosmology.h:16: error: expected ‘;’ before ‘double’
Cosmology.h:17: error: floating-point literal cannot appear in a constant-expression
Cosmology.h:17: warning: ISO C++ forbids initialization of member constant ‘H0’ of non-integral type ‘const double’
Cosmology.h:17: error: floating-point literal cannot appear in a constant-expression
Cosmology.h:17: error: floating-point literal cannot appear in a constant-expression
Cosmology.h:17: error: ‘Cosmology::H0’ cannot appear in a constant-expression
Cosmology.h:17: error: ‘Cosmology::H0’ cannot appear in a constant-expression
Cosmology.h:17: warning: ISO C++ forbids initialization of member constant ‘OmegaM’ of non-integral type ‘const double’
Cosmology.h:17: error: floating-point literal cannot appear in a constant-expression
Cosmology.h:17: warning: ISO C++ forbids initialization of member constant ‘OmegaL’ of non-integral type ‘const double’
Cosmology.h:17: error: floating-point literal cannot appear in a constant-expression
Cosmology.h:17: warning: ISO C++ forbids initialization of member constant ‘OmegaG’ of non-integral type ‘const double’
Cosmology.h:17: error: floating-point literal cannot appear in a constant-expression
Cosmology.h:17: warning: ISO C++ forbids initialization of member constant ‘OmegaK’ of non-integral type ‘const double’
Cosmology.h: In static member function ‘static double Cosmology::CCallback(double, void*)’:
Cosmology.h:23: error: ‘struct CCallbackHolder’ has no member named ‘cls’
Last edited on
cosmology.h line 7: The type Cosmology has not yet been defined. What you need is a forward declaration.

3
4
//  Forward declaration 
class Cosmology;


cosmology.h line 13: constexpr is a C++11 feature. Are you sure you're using a C++11 enabled compiler and have the option turned on?

cosmology.h line 14: Again, initialization is a C++11 feature. Looks like you're not using a C++11 enabled compiler.

cosmology.h line 23: No member "cls". This will go away when you include the forward declaration.
so if I substitute the static constexpr double with static const double would the problem get solved?
You can initialize a static const without C++11.
Topic archived. No new replies allowed.