0

I am a newbie in 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 by looking up in internet

Update: Cosmology.h

#include <cmath>
#include <gsl/gsl_integration.h>


struct CCallbackHolder
{
  Cosmology* cls;
  void* data;
};

class Cosmology {
private:
    static const 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();
    Cosmology(double);
    Cosmology(double , double );
    Cosmology(double , double , double );
    Cosmology(double , double , double , double );
    Cosmology(double , double , double , double , double );
    double distC(double);
  } cosmo;

Cosmology.cpp

#include <cmath>
#include <gsl/gsl_integration.h>
#include "Cosmology.h"
#include<iostream>
using namespace std;
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;
}

I am getting the following errors when I tried to compile the code:

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: ISO C++ forbids declaration of ‘constexpr’ with no type
Cosmology.h:17: error: expected ‘;’ before ‘double’
In file included from Universe.cpp:3:
Cosmology.h: In static member function ‘static double Cosmology::CCallback(double, void*)’:

by compiling with this line : g++ -Wall -pedantic Cosmology.cpp -o Cosmology std=c++0x. How could I fix the code?

Dalek
  • 3,827
  • 8
  • 34
  • 78
  • Write the comments in the lines that the errors point to. – KjMag Jun 30 '14 at 12:04
  • Deleted my answer since I've forgotten that you can only do it with the integral types, not double. Also, as asked before: mark the lines that the errors refer to by adding comments. – KjMag Jun 30 '14 at 12:33
  • Just use a wrapper cosmologist and avoid problems with static functions in your class (you can't avoid static functions, but you can avoid to have it in your class)! See http://stackoverflow.com/a/18181494/2472169 – Vivian Miranda Jun 30 '14 at 15:20
  • By the way, it makes no sense to write the physical constants yourself (like the speed of light) given that GSL provides them https://www.gnu.org/software/gsl/manual/html_node/Physical-Constants.html . But if you want to, double has 16 digits of precision so try to write conversions with that precision. Avoid unnecessary problems with propagation of errors - just a general guideline. – Vivian Miranda Jun 30 '14 at 15:27

1 Answers1

0

The main problem with your program is that you initialize non-static variables that are class members while declaring them, which is not a legal C++ operation. To be specific, the problem are these lines:

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;

c, Mpc2Km, Yrs2Sec, H0, OmegaM, OmegaL, OmegaG, OmegaK are non-static, non-const and double. If you want to initialize them (i.e. assign values to them), you have to do it within the Cosmology class constructor or move them outside of the class declaration and thus make them global, but note that you should avoid global variables unless you know what you do and there's no other way.

If you want to initialize them in the header file like you tried to do, they must be of integral type - won't work for doubles.

Shortly: the first line won't work because the type is double (only integral static const types can be initialized inside class declaration); the second won't work since the type is non-const, non-static and double.

The rest of the errors will probably vanish as soon as these lines are fixed.

KjMag
  • 2,375
  • 13
  • 16
  • I've updated the answer. Also, please don't just edit your question by replacing text, but, if you modify it, write what you did and what are the new errors - this way it's easier to follow how the problem develops. – KjMag Jun 30 '14 at 12:49
  • could you please explain with more detail, the changes I have to make? Because I tried to fix it but got more errors. – Dalek Jun 30 '14 at 13:07
  • In your code you have, for example, the function Cosmology() - this is the class constructor. You can assign values to these variables inside this function in Cosmology.cpp file. In class declaration, i.e. in the Cosmology.h file, leave only the name of the variables (look at how DistH and TimeH are declared and do the same with the problematic variables in the .h file). If you don't know what I am talking about, you may want to read some C++ book, because these are pretty basic staff and if you have problems with that, you will get stuck immediately after solving this particular problem. – KjMag Jun 30 '14 at 13:19