I have the following class defined in code -
-
//header file
-
-
class froot{
-
public:
-
void calculate_root(int i);
-
double F(double x);
-
double G(double x);
-
-
double find_root(double (*g)(double),double x1,double x2,int &depth);
-
double root(double x1,double x2,double (*g)(double),double f1,double f2,int &depth);
-
-
private:
-
-
};
-
-
//Source File
-
-
double froot::find_root(double (*g)(double), double x1, double x2, int &depth)
-
{
-
double f1=(*g)(x1);
-
double f2=(*g)(x2);
-
-
if(f1*f2>0.0){
-
cout<<"ERROR in find_root(): values at end-points have same sign"<<endl;
-
exit(EXIT_FAILURE);
-
return 0;
-
}
-
else if(x2-x1>0.0)
-
return root(x1,x2,*g,f1,f2,depth);
-
else
-
return root(x2,x1,*g,f2,f1,depth);
-
}
-
-
double froot::root(double x1, double x2, double (*g)(double), double f1, double f2, int &depth)
-
{
-
//This function calculates the root of a function using the bisection method
-
const int max_depth=1000;
-
const double x_limit=1e-12;
-
double estimated_root;
-
double x_mid=(x1+x2)*0.5;
-
-
if(x2-x1<=x_limit)
-
estimated_root=x_mid;
-
else if(++depth>max_depth){
-
cout<<"WARNING: Maximum limit of "<<max_depth<<" bisections reached in root().\n";
-
estimated_root=x_mid;
-
}
-
else{
-
double f_mid=(*g)(x_mid);
-
if(f_mid==0.0){
-
//Zero at x_mid
-
estimated_root=x_mid;
-
}
-
else if((*g)(x1)*f_mid<0.0){
-
//Zero in first segment
-
estimated_root=root(x1,x_mid,*g,f1,f_mid,depth);
-
}
-
else{
-
//Zero in second segment
-
estimated_root=root(x_mid,x2,*g,f_mid,f2,depth);
-
}
-
}
-
return estimated_root;
-
}
-
-
double froot::F(double x)
-
{
-
return exp(x)+pow(2.0,-x)+2.0*cos(x)-6.0;
-
}
-
-
double froot::G(double x)
-
{
-
return x-cos(x);
-
}
-
-
void froot::calculate_root(int i)
-
{
-
int depth;
-
double sol;
-
-
if(i==1){
-
depth=0;
-
sol=find_root(F,1.0,2.0,depth);
-
}
-
else{
-
depth=0;
-
sol=find_root(G,0.0,PI/2,depth);
-
}
-
}
-
-
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.
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.
@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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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);
//....
};
|
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,...
|
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;...
|
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...
|
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...
| |
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...
|
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...
|
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...
|
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,...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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 ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |