test.C: In member function ‘void Field::Evaluate()’:
test.C:29: error: no matching function for call to ‘Field:: Diff(double&, <unknown type>)’
test.C:18: note: candidates are: double Field:: Diff(double, double (*)(double))
Why does it not recogize the type? What can I do to correct the situation? I think it is because the function MyMathFunction is not defined in the scope of the function Diff but I don't know how to fix that. I wrote a small sample of code that exhibits the behavior I am trying to correct. My compiler version is
g++ (GCC) 4.0.2 20051125 (Red Hat 4.0.2-8)
Thanks in advance.
Expand|Select|Wrap|Line Numbers
- #include <iostream.h>
- #include <math.h>
- class Field{
- public:
- double MyMathFunction1( double );
- // In the function below I pass x and f(x) separately
- double Diff( double, double(*)(double) );
- void Evaluate();
- };
- double Field::MyMathFunction1(double x){ return sin(x);}
- double Field::Diff( double x, double(*func)(double) )
- {
- // The idea is that func(x) becomes MyMathFunction1(x)
- double h=1e-6;
- return (func(x+h)-func(x-h))/2./h;
- }
- void Field::Evaluate(){
- // Use some dummy value of x for testing
- double x=1.5707963;
- // pass the function MyMathfunction1(x) to Diff(double,double(*)() )
- cout << "Dsin(x)|x="<<x<<":\t"<<Diff( x, MyMathFunction1 )<<endl;
- }
- int main()
- {
- Field * DoAnalysis = new Field;
- DoAnalysis->Evaluate();
- return 0;
- }