Home > Software engineering >  How to call non static member function from static member function in this particular case
How to call non static member function from static member function in this particular case

Time:12-28

I would like to call the xupv() method from inside func(), but it fails with the following error:

error: cannot call member function ‘auto f(double, double)::Local_::xupv(double)’ without object

#include <iostream>
#include <iomanip>
#include <cmath>
#include <fstream>
#include "QCDNUM/QCDNUM.h"
#include <boost/math/quadrature/trapezoidal.hpp>
#include <vector>

using boost::math::quadrature::trapezoidal;

using namespace std::complex_literals;
using std::cout;
using std::end;

double M = 932.17;
double B = 1.0 / 73.0;
double m_u = 162;
double m_d = 81;
double V = 4.0 / 3.0 * 3.14159 * pow(0.0056, 3);
double R = 0.0056;

auto f(double m, double n){

    class Local_
    {
    public:
      int a;
      int b;
      // All local functions go here:
      //
      auto xupv( double x )
      {
         auto z = [&](double E){
            auto a_1 = a*b*E
            return a_1 
         };
         auto e = trapezoidal(z, x * M / 2.0, M / 2.0);
         auto d = pow(x, 2) * pow(M, 2) / 2.0;
         return e * d;
      };


      static auto func(int *ipdf, double *x)
      {
        int i = *ipdf;
        double xb = *x;
        double f = 0;
        if (i == 0)
            f = 0;
        if (i == 1)
            f = xupv(xb);
        if (i == 2)
            f = 0;
        if (i == 3)
            f = 0;
        if (i == 4)
            f = 0;
        if (i == 5)
            f = 0;
        if (i == 6)
            f = 0;
        if (i == 7)
            f = 0;
        if (i == 8)
            f = 0;
        if (i == 9)
            f = 0;
        if (i == 10)
            f = 0;
        if (i == 11)
            f = 0;
        if (i == 12)
            f = 0;
        return f;
      };
    } local;

    local.a = m;
    local.b = n;

// -----------------------------------------------------------

    int ityp = 1, iord = 3, nfin = 0;                       // unpol, NLO, VFNS
    double as0 = 0.364, r20 = 2.0;                          // input alphas
    double xmin[] = {1.e-4, 1.e-3, 1.e-2, 1.e-1};           // x-grid
    int iwt[] = {1, 1, 1, 1}, ng = 4, nxin = 100, iosp = 3; // x-grid
    int nqin = 60;                                          // mu2-grid
    double qq[] = {1e0, 15}, wt[] = {1e0, 1e0};             // mu2-grid
    double q2c = 3, q2b = 25, q0 = 1;                       // thresholds, mu20
    double x = 1, q = 15, qmz2 = 8315.25, pdf[13];

    double def[] = // input flavour composition
        // tb  bb  cb  sb  ub  db   g   d   u   s   c   b   t
        {0., 0., 0., 0., 0., -1., 0., 1., 0., 0., 0., 0., 0., // 1=dval
         0., 0., 0., 0., -1., 0., 0., 0., 1., 0., 0., 0., 0., // 2=uval
         0., 0., 0., -1., 0., 0., 0., 0., 0., 1., 0., 0., 0., // 3=sval
         0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0.,  // 4=dbar
         0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0.,  // 5=ubar
         0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0.,  // 6=sbar
         0., 0., -1., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., // 7=cval
         0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,  // 8=cbar
         0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,  // 9=zero
         0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,  // 10=zero
         0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,  // 11=zero
         0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.}; // 12=zero

    int nx, nq, id1, id2, nw, nfout, ierr;
    double eps;
    int lun = 6;
    string outfile = " ";

    QCDNUM::qcinit(lun, outfile);                    // initialize
    QCDNUM::gxmake(xmin, iwt, ng, nxin, nx, iosp);   // x-grid
    QCDNUM::gqmake(qq, wt, 2, nqin, nq);             // mu2-grid
    QCDNUM::wtfile(1, "../weights/unpolarised.wgt"); // calculate weights
    QCDNUM::setord(iord);                            // LO, NLO, NNLO
    QCDNUM::setalf(as0, r20);                        // input alphas
    int iqc = QCDNUM::iqfrmq(q2c);                   // charm threshold
    int iqb = QCDNUM::iqfrmq(q2b);                   // bottom threshold
    QCDNUM::setcbt(nfin, iqc, iqb, 999);             // thresholds in the VFNS
    int iq0 = QCDNUM::iqfrmq(q0);                    // start scale

    QCDNUM::evolfg(11, local.func, def, iq0, eps);           // evolve all pdf's

    QCDNUM::allfxq(1, x, q, pdf, 0, 1);              // interpolate all pdf's
    auto res = (pow(2.0 / 3.0, 2)) * (pdf[8]   pdf[4])   (pow(1.0 / 3, 2)) * (pdf[7]   pdf[5])   (pow(1 / 3.0, 2)) * (pdf[9]   pdf[3]);
    cout << res << endl;
}

int main()
{
    f(5, 6);
    return 0;
}

I tried making func() non-static and using this, but then func() cannot be passed to this line below:

QCDNUM::evolfg(11, local.func, def, iq0, eps);

CodePudding user response:

The only way for the static func() to use the non-static xupv() is to give func() access to the local object. Passing the object as a parameter is not an option with the QCDNUM library, so you will have to use a global/static pointer instead, eg:

// move this into global scope...
class Local_
{
public:
  ...
  static Local_ *pThis; // <-- add this

  Local_() { pThis = this; }

  auto xupv( double x )
  { 
     return ...;
  }

  static auto func(int *ipdf, double *x)
  {
     ...
     if (i == 1)
        f = pThis->xupv(xb); // <-- use it here
     ... 
     return ...;
  } 
}; 

Local_* Local_::pThis = nullptr;

auto f(double m, double n){

    Local_ local;
    ...
    QCDNUM::evolfg(..., local.func, ...);
    ...
}
  • Related