473,507 Members | 8,335 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Pointers to Class Members

emaghero
85 New Member
I have the following class defined in code

Expand|Select|Wrap|Line Numbers
  1.  
  2. //header file
  3.  
  4. class froot{
  5. public:
  6.     void calculate_root(int i);
  7.     double F(double x);
  8.     double G(double x);
  9.  
  10.     double find_root(double (*g)(double),double x1,double x2,int &depth);
  11.     double root(double x1,double x2,double (*g)(double),double f1,double f2,int &depth);
  12.  
  13. private:
  14.  
  15. };
  16.  
  17. //Source File
  18.  
  19. double froot::find_root(double (*g)(double), double x1, double x2, int &depth)
  20. {
  21.     double f1=(*g)(x1);
  22.     double f2=(*g)(x2);
  23.  
  24.     if(f1*f2>0.0){
  25.         cout<<"ERROR in find_root(): values at end-points have same sign"<<endl;
  26.         exit(EXIT_FAILURE);
  27.         return 0;
  28.     }
  29.     else if(x2-x1>0.0)
  30.         return root(x1,x2,*g,f1,f2,depth);
  31.     else
  32.         return root(x2,x1,*g,f2,f1,depth);
  33. }
  34.  
  35. double froot::root(double x1, double x2, double (*g)(double), double f1, double f2, int &depth)
  36. {
  37.     //This function calculates the root of a function using the bisection method
  38.     const int max_depth=1000;
  39.     const double x_limit=1e-12;
  40.     double estimated_root;
  41.     double x_mid=(x1+x2)*0.5;
  42.  
  43.     if(x2-x1<=x_limit)
  44.         estimated_root=x_mid;
  45.     else if(++depth>max_depth){
  46.         cout<<"WARNING: Maximum limit of "<<max_depth<<" bisections reached in root().\n";
  47.         estimated_root=x_mid;
  48.     }
  49.     else{
  50.         double f_mid=(*g)(x_mid);
  51.         if(f_mid==0.0){
  52.             //Zero at x_mid
  53.             estimated_root=x_mid;
  54.         }
  55.         else if((*g)(x1)*f_mid<0.0){
  56.             //Zero in first segment
  57.             estimated_root=root(x1,x_mid,*g,f1,f_mid,depth);
  58.         }
  59.         else{
  60.             //Zero in second segment
  61.             estimated_root=root(x_mid,x2,*g,f_mid,f2,depth);
  62.         }
  63.     }
  64.     return estimated_root;
  65. }
  66.  
  67. double froot::F(double x)
  68. {
  69.     return exp(x)+pow(2.0,-x)+2.0*cos(x)-6.0;    
  70. }
  71.  
  72. double froot::G(double x)
  73. {
  74.     return x-cos(x);    
  75. }
  76.  
  77. void froot::calculate_root(int i)
  78. {
  79.     int depth;
  80.     double sol;
  81.  
  82.     if(i==1){
  83.         depth=0;
  84.         sol=find_root(F,1.0,2.0,depth);
  85.     }
  86.     else{
  87.         depth=0;
  88.         sol=find_root(G,0.0,PI/2,depth);
  89.     }
  90. }
  91.  
  92.  
When I try to build the project I get an error in the calculate_root function.

It's telling me to replace F with &froot::F and to replace G with &froot::G, but this doesn't work.

I can kind of see why there is a problem, in that I'm trying to call find_root by passing a class member when it is expecting a pointer to a function

How should I change the syntax of the find_root, and root, declaration so that it will accept functions which are declared inside the same class?

Thanks for your help.

Any sugestions greatly appreciated.
Feb 17 '09 #1
2 1394
Savage
1,764 Recognized Expert Top Contributor
Member functions cannot be used like that to represent callback functions.You must make them either static functions,or create wrapper function outside the class which will call the objects method.That's one of the reasons why functors(objects with overloaded function call operators,so that they behave just like a functions) are a better choice than callback functions.
Feb 17 '09 #2
emaghero
85 New Member
@Savage
So, is it the case that I can't do what I was trying to do unless I use functors?

I guess I better get reading then.

Thanks very much for your help.
Feb 17 '09 #3

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

Similar topics

4
1758
by: Oystein Haare | last post by:
Is it best to declare class member objects as pointers or not pointers? class A { public: A(int i); //.... };
3
1212
by: m | last post by:
Hi, I'm reading Microsoft Visual C++ .NET Step by Step, Version 2003 and I found a sentence it says : "you can't have pointers to managed types as members of a __gc class.". Why ? Thanks,...
20
2087
by: Joe Van Dyk | last post by:
Is there some rule of thumb about when to use pointers to an object and when to use a reference* to an object when a class needs to have objects as data members? Example: class A { B* b_ptr;...
4
3494
by: Josefo | last post by:
Hello, is someone so kind to tell me why I am getting the following errors ? vector_static_function.c:20: error: expected constructor, destructor, or type conversion before '.' token...
1
2835
by: FNA access | last post by:
Hello all, I am somewhat new to C++, so please forgive my ignorance. I have an assignment that I am working on and while I do not want someone to do my homework for me, some advice to point me in...
0
7109
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7372
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7481
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5619
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1537
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
758
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
411
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.