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...
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).
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?