0

When I compile the following:

#include "stdafx.h"
#define _USE_MATH_DEFINES
#include <cmath>
#include <iostream>

using namespace std;

#define A 10

double psi(double x);
double qgaus(double(*func)(double), double a, double b);
double normalize();

double psi(double x) {
    return pow(sin(M_PI_2*x/ A), 2);
}

double qgaus(double(*func)(double), double a, double b) {
    double xr, xm, dx, s;
    static double x[] = { 0.0, 0.1488743389, 0.4333953941, 0.6794095682,0.8650633666,0.9739065285 };
    static double w[] = { 0.0, 0.2955242247, 0.2692667193,  0.2190863625,0.1494513491,0.0666713443 };
    xm = 0.5*(b + a);
    xr = 0.5*(b - a);
    s = 0;
    for (int j = 1; j <= 5; j++) {
        dx = xr*x[j];
        s += w[j] * ((*func)(xm + dx) + (*func)(xm - dx));
    }
    return s *= xr;
}

double normalize() {
    double N;
    N = 1 / sqrt(qgaus(psi, 0.0, A));
        return N;
}

int main()
{
    double Norm = normalize();
    cout << Norm << endl;
    return 0;
}

This code compiles without an error. However when I try to put two of the routines into a class as shown here with the changes.

class PsiC {
public:
    double psi(double x);
    double normalize();
};

double PsiC::normalize() {
    PsiC my_psi;
    double N;
    N = 1 / sqrt(qgaus(my_psi.psi, 0.0, A));                        
    return N;
}

Now using the following in main:

PsiC my_psi;
double Norm = my_psi.normalize();
cout << Norm << endl;

The statement N = 1 / sqrt(qgaus(my_psi.psi, 0.0, A)); gives the compiler error:

'func': function call missing argument list; use '&func' to create a pointer to member.

Note: I only have two member in this class now; however, I intend to add more member later.

Cœur
  • 37,241
  • 25
  • 195
  • 267
James N
  • 84
  • 2
  • 11
  • 3
    Have you considered doing what the compiler helpfully suggested? – nwp Jan 20 '17 at 16:08
  • 3
    You should not be creating a second instance of `PsiC` inside `PsiC::normalize` – drescherjm Jan 20 '17 at 16:12
  • 3
    make `psi` `static`.... or.. dunno.. TBH I have no idea why you've created a class. – Karoly Horvath Jan 20 '17 at 16:28
  • 1
    It's really not clear why you created a class for a couple of functions that can be static... – roalz Jan 20 '17 at 16:51
  • 1
    qgaus() takes a normal function pointer, but the function you are trying to pass in now resides in a class. Pointers to class member functions are a bit different. There was another quetion on this: www.stackoverflow.com/questions/2402579/function-pointer-to-member-function – rcs Jan 20 '17 at 17:00
  • Are you seriously still using `define` for constant in C++... and still declaring your variable at the top of your functions. Why you don't use C++ features that fixes C problematic areas like constant and declaration at the point of use? – Phil1970 Jan 20 '17 at 17:25
  • What is wrong with using a few define constants? – James N Jan 20 '17 at 17:30
  • Clearly, you have not read much books on writing good C++ code as it is well known that `define` are evil and should be avoided whenever possible. `Define` does not respect scoping rules (namespace) and have global effect. Any real C++ programmer would write `const int A = 10;` and would probably also use a meaningful name. – Phil1970 Jan 20 '17 at 17:41
  • Thanks Phil, I will use advise and change my code. I am somewhat new to C++. I originally learned C twenty five years ago. – James N Jan 20 '17 at 17:47
  • By the way, compiler errors are well documented in the documentation: [Compiler Error C3867](https://msdn.microsoft.com/en-us/library/b0x1aatf.aspx). – Phil1970 Jan 20 '17 at 17:53

0 Answers0