编译以下内容时:
#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;
}
此代码编译时没有错误。但是,当我尝试将两个例程放入一个类中时,如此处所示的更改。
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;
}
现在在main中使用以下内容:
PsiC my_psi;
double Norm = my_psi.normalize();
cout << Norm << endl;
声明N = 1 / sqrt(qgaus(my_psi.psi,0.0,A));给出编译错误:
'func': function call missing argument list; use '&func' to create a pointer to member.
注意:我现在只有两个成员;但是,我打算稍后再添加更多成员。