function pointers to other class functions

Hey guys, I am a brand new beginner at C++ so please be nice as this stuff is scary!

Basically I have 2 classes, one for computing numerical integration and the other class for creating polynomials.

I am trying to link the 2 classes in my main file by creating a function in my numerical integration class to set the function I want to integrate.

Here's my header file for numerical int:

#ifndef numericalint_h
#define numericalint_h
#include "polynomial.h"

class DefInt
{

private:
int n; // integer number used by numerical integration techniques
double a,b; //lower,upper bound of integral
double (*function)(double x,int p,double *polyptr); //pointer to the function you want to integrate

public:
void seta(double a_){a=a_;} //to be able to set the private variable a
void setb(double b_){b=b_;} //""
void setn(double n_){n=n_;}//""
double ByTrapezoid(int p, double *polyptr);//numerical int alogrithm
double BySimpson(int p, double *polyptr);//""
void Setfunction(double (*function_)(double x,int p,double *polyptr)){function=function_;}//to be able to set the private function pointer

};


#endif

Here's my header file for the polynomial class:

#ifndef polynomial_h
#define polynomial_h

class Polynomial
{
private:
int n;//degree of polynomial
double *polyptr; //i use dynamic memory for storing the polynomial, this is the pointer to the dynamic memory

public:

void setn(double n_){n=n_;}
void setpolyptr(double *polyptr_){polyptr=polyptr_;}
void enterpolynomial();
double evaluationpolynomial(double x);//this is equivalent to the function I want to integrate
double analytical(double a,double b);

};


#endif


So in my main file I call the set function from my numerical integration class by: MyInt.setfunction(MyPoly.evaluationpolynomial);

where MyInt and MyPoly are objects of the 2 classes (if thats how you call it!?)

I get the following error message:

D:\Ex10\Main.cpp(20) : error C2664: 'Setfunction' : cannot convert parameter 1 from 'double (double)' to 'double (__cdecl *)(double,int,double *)'

The weird thing is that when I don't create a polynomial class, just create equivalent functions it works fine...

Any ideas? Cheers
First of all, the function you are using inside Polynomial (evaluationpolynomial) doesn't even take the same
number of parameters as the function that DefInt wants.

But secondly, pointers to functions and pointers to member functions are not the same thing; they
have different types even if the types and number of parameters are the same. To do what you want,
you will need to make Polynomial::evaluationpolynomial a non-member function (declare it static is one
way).
Thanks a lot for the reply.

RE: "First of all, the function you are using inside Polynomial (evaluationpolynomial) doesn't even take the same number of parameters as the function that DefInt wants."

I thought that the private variables of a class become arguments of the public member functions so Polynomial::evaluationpolynomial(double x) takes arguments double x, int n, double *polyptr? But I guess that's not correct...

I was trying to encapsulate everything about polynomials in one class, so is there no way of keeping Polynomial::evaluationpolynomial a public member of the class and maybe changing the function pointer in DefInt?

Cheers
PS Do you think this is the right way of setting up this problem, I am trying to think in the "obejct oriented" way ? Any tips ?
Topic archived. No new replies allowed.