jeudi 1 octobre 2015

nested functions in C++

I wish to use nested function in C++ in order to avoid global variables. I found a method for doing that by using classes (http://ift.tt/1jzZjeW). I tried to implement that solution but I failed. Here is my code :

class Myclass 
{
    double (*retval)(double, double);
double A;
double x0, y0, z0, z, a, b,k;
int mu, nu, s, t;

double g(double x, double y)
{
    return (x0+y0+z0+a+b+k+s+t+mu+nu)*A*x*y;
}
public :
Myclass(complex<double> A, double x0, double y0, double z0, double k, double a, double b,  int mu, int nu, int s, int t) : z(0.)
{
    this->A=A;
    this->x0=x0;
    this->y0=y0;
    this->z0=z0;
    this->k=k;
    this->a=a;
    this->b=b;
    this->mu=mu;
    this->nu=nu;
    this->s=s;
    this->t=t;
    retval=g;
}
operator double (double, double)
{
    return retval;
}
};

int main ()
{
    /*
    ....
   */
    return 0;
}

The goal is to return a function with the prototype double (double, double) which will be used for by other instructions.

I got this errors messages :

g++ -c -g -MMD -MP -MF "build/Debug/GNU-Linux-x86/main.o.d" -o build/Debug/GNU-Linux-x86/main.o main.cpp main.cpp:114:36: error: ‘Myclass::operator double(double, double)’ must take ‘void’ operator double (double, double) ^ main.cpp: In constructor ‘Myclass::My_class(std::complex, double, double, double, double, double, double, int, int, int, int)’: main.cpp:112:15: error: cannot convert ‘Myclass::g’ from type ‘double (champ_speculaire_class::)(double, double)’ to type ‘double (*)(double, double)’ retval=g; ^

Aucun commentaire:

Enregistrer un commentaire