How to pass a function name as an argument?

Y

Yangang Bao

I have a simple test program. It doesn't work. I want to know what is the
reason and how to
fix it. Maybe I should use template function to do it. But I don't know how.

Here is the simple program. Strange enough that the problem never happens in
C languge,
i.e., in the program, if I call "foo2", it works fine. Why "foo1" doesn't
work?

Thanks for your answers.
////////////////////////////////////////////////////////////////////////////
/
file://main.cpp
////////////////////////////////////////////////////////////////////////////
/
#include <stdio.h>
#include <stdlib.h>
#include "Funct.h"

void main()
{
double s;
Funct funct;
s = funct.drive();
printf("Result= %lf", s);
}

////////////////////////////////////////////////////////////////////////////
/
file://Funct.h
////////////////////////////////////////////////////////////////////////////
/
double foo2(double t);

class Funct
{
double Integrate(double(*func)(double), double a, double b);
double foo1(double t);

public:
double drive();

};
////////////////////////////////////////////////////////////////////////////
/
file://Funct.cpp
////////////////////////////////////////////////////////////////////////////
/
#include "Funct.h"

double Funct::Integrate(double (*func)(double), double a, double b)
{
double c = a+b;
return func(c);
}

double Funct::foo1(double t)
{
return t*t;
}

double Funct::drive()
{
double t = Integrate(foo1, 2, 3);
// double t = Integrate(foo2, 2, 3);
return t;
}

double foo2(double t)
{
return t*t;
}
**************************************
 
J

John Carson

Yangang Bao said:
I have a simple test program. It doesn't work. I want to know what is
the reason and how to
fix it. Maybe I should use template function to do it. But I don't
know how.

Here is the simple program. Strange enough that the problem never
happens in C languge,

The C language doesn't have member functions, so the issue never arises.
i.e., in the program, if I call "foo2", it works fine. Why "foo1"
doesn't work?

Because it is a member function, not a "free" function. The syntax for
pointers to member functions is very different to the syntax for pointers to
ordinary functions.

[snip]
class Funct
{
double Integrate(double(*func)(double), double a, double b);

// Try the following overload:

double Integrate(double(Funct::*func)(double), double a, double b);

// which you define as

double Funct::Integrate(double (Funct::*func)(double), double a, double b)
{
double c = a+b;
return (this->*func)(c);
}
 
J

Josephine Schafer

Yangang Bao said:
I have a simple test program. It doesn't work. I want to know what is the
reason and how to
fix it. Maybe I should use template function to do it. But I don't know how.

Here is the simple program. Strange enough that the problem never happens in
C languge,
i.e., in the program, if I call "foo2", it works fine. Why "foo1" doesn't
work?

Thanks for your answers.
////////////////////////////////////////////////////////////////////////////
/
file://main.cpp
////////////////////////////////////////////////////////////////////////////
/
#include <stdio.h>
#include <stdlib.h>
#include "Funct.h"

void main()
{
double s;
Funct funct;
s = funct.drive();
printf("Result= %lf", s);
}

////////////////////////////////////////////////////////////////////////////
/
file://Funct.h
////////////////////////////////////////////////////////////////////////////
/
double foo2(double t);

class Funct
{
double Integrate(double(*func)(double), double a, double b);
double foo1(double t);

public:
double drive();

};
////////////////////////////////////////////////////////////////////////////
/
file://Funct.cpp
////////////////////////////////////////////////////////////////////////////
/
#include "Funct.h"

double Funct::Integrate(double (*func)(double), double a, double b)
{
double c = a+b;
return func(c);
}

double Funct::foo1(double t)
{
return t*t;
}

double Funct::drive()
{
double t = Integrate(foo1, 2, 3);
// double t = Integrate(foo2, 2, 3);
return t;
}

double foo2(double t)
{
return t*t;
}

See the first parameter of Integrate.
It is a pointer to a function that takes a double as parameter and returns a
double.
What is the type of foo1?
It is double (Funct::*)(double).
Pointers to non-static member functions are not type compatible with regular
pointers to functions, hence the error.

HTH,
J.Schafer
 
Y

Yangang Bao

I tried to adopt this method in my program. And unfortunately a tragedy
happens. The problem is that I have to pass the function name as argument
twice.
And the VC6.0 compiler still shows the wrong message: INTERNAL COMPILER
ERROR. I just wonder how other guys implemented the quadrature
functions in C++.

Here is the simple test program:
//////////////////////////////////////////////////////////////////////////
// main.cpp
//////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <stdlib.h>

#include "Funct.h"

void main()
{
Funct funct;
funct.drive();
}

//////////////////////////////////////////////////////////////////////////
// Funct.h
//////////////////////////////////////////////////////////////////////////
#ifndef FUNCT_INCLUDED_
#define FUNCT_INCLUDED_

class Funct
{
public:
double Quadrature(double (Funct::*func)(double), double a, double b);
double QuadStep(double (Funct::*func)(double), double c, double d);

double foo1(double t);

double drive();
};

#endif

//////////////////////////////////////////////////////////////////////////
// Funct.cpp
//////////////////////////////////////////////////////////////////////////
#include "Funct.h"

double Funct::drive()
{
double a = 1;
double b = 2;
double s;

s = Quadrature(foo1, a, b);
return s;

}

double Funct::foo1(double t)
{
return t*t;
}

double Funct::Quadrature(double (Funct::*func)(double), double a, double b)
{
double c= (a+b)/2.0;
double s1, s2;

s1 = QuadStep((this->*func), a, c);
s2 = QuadStep((this->*func), c, d);
return s;
}

double Funct::QuadStep(double (Funct::*func)(double), double c, double d)
{
return (this->*func)(c*d);
}
 
T

tom_usenet

I tried to adopt this method in my program. And unfortunately a tragedy
happens. The problem is that I have to pass the function name as argument
twice.
And the VC6.0 compiler still shows the wrong message: INTERNAL COMPILER
ERROR. I just wonder how other guys implemented the quadrature
functions in C++.
double Funct::Quadrature(double (Funct::*func)(double), double a, double b)
{
double c= (a+b)/2.0;
double s1, s2;

s1 = QuadStep((this->*func), a, c);
s2 = QuadStep((this->*func), c, d);

The above two should be:

s1 = QuadStep(func, a, c);
s2 = QuadStep(func, c, d);

You only dereference the pointer to member when calling it.
return s;
}

double Funct::QuadStep(double (Funct::*func)(double), double c, double d)
{
return (this->*func)(c*d);

That's fine, since you're calling it.

Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top