473,320 Members | 1,883 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Help needed with scope resolution

I have a piece of code in which I pass a function to a function and it works fine in stand alone code but when I encorporate it in a class my compiler tells me there is no matching function. The error message is:

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
  1. #include <iostream.h>
  2. #include <math.h>
  3.  
  4. class Field{
  5.  public:
  6.  
  7. double MyMathFunction1( double );
  8. // In the function below I pass x and f(x) separately
  9. double Diff( double, double(*)(double) ); 
  10.  
  11.  void Evaluate();
  12. };
  13.  
  14. double Field::MyMathFunction1(double x){  return sin(x);}
  15. double Field::Diff( double x, double(*func)(double) )
  16. {
  17.   // The idea is that func(x) becomes  MyMathFunction1(x) 
  18.  
  19.   double h=1e-6;
  20.   return (func(x+h)-func(x-h))/2./h;
  21. }
  22. void Field::Evaluate(){
  23.   // Use some dummy value of x for testing
  24.   double x=1.5707963;
  25.   // pass the function MyMathfunction1(x) to Diff(double,double(*)() )
  26.   cout << "Dsin(x)|x="<<x<<":\t"<<Diff( x, MyMathFunction1 )<<endl;
  27. }
  28.  
  29. int main()
  30. {
  31.  
  32.   Field * DoAnalysis = new Field;
  33.   DoAnalysis->Evaluate();
  34.  
  35.   return 0;
  36. }
  37.  
Oct 15 '06 #1
1 1426
tyreld
144 100+
Why are you using a function pointer when the function is already a member of the class? Why not just call the function directly?
Oct 16 '06 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: exits funnel | last post by:
Hello, I'm confused by the use of the Scope resolution operator on the indicated lines in the following code (which was copied from Thinking in C++ by Bruce Eckel). Removing them seems to have...
0
by: Howard Gardner | last post by:
/* I don't have to provide scope resolution in sit 0 below: adl finds the function that I mean to use. I do have to provide scope resolution in sits 1 and 2. Can anyone teach me a trick to...
3
by: richard pickworth | last post by:
Hello :) I am familiar with using scope resolution operator to define classes, but why does it turn up elsewhere? thanks richard
11
by: milkyway | last post by:
Hello, I have an HTML page that I am trying to import 2 .js file (I created) into. These files are: row_functions.js and data_check_functions.js. Whenever I bring the contents of the files into...
16
by: sushant | last post by:
hi , can we use scope resolution operator (::) in C??? sushant
6
by: Jack | last post by:
I want to write code that can be compiled as C and C++. In the Windows API, there are many macros that help with scope resolution. For example: #ifdef __cplusplus #define SNDMSG ::SendMessage...
7
by: John Salerno | last post by:
I have the following code: class DataAccessFrame(wx.Frame): menu_items = def __init__(self): wx.Frame.__init__(self, None, title='Database Access Panel')
6
by: Subra | last post by:
Hi, If I have the same varible defined in global as well as in local scope, how to access global scope varible in a function having the same local def. #include<stdio.h> static int a=25;...
5
by: Shan | last post by:
In the following code: func() { int SmeVar; { int SmeVar; //Here I want to use SmeVar defined in the outer block how can I do that ? }
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.