Call a Function From One Class Inside Another Class's Function

Suppose I have the following 2 class header files (A.h and B.h), with a function inside each:

1
2
3
4
5
class A
{
public:
double f(double);
};


1
2
3
4
5
class B
{
public:
double g(double);
};


Here are their corresponding cpp files:

1
2
3
4
5
6
#include "A.h"

double A::f(double variable1)
{
// do stuff on variable 1
}


1
2
3
4
5
6
7
#include "B.h"
#include "A.h"

double B::g(double variable2)
{
// do stuff on variable2 using f from A
}


I would like to use function f (from A) inside function g (in B). How do I do this? I tried putting A::f() inside of g, but this doesn't work. Thanks!
You would need an instance of A to use function f.

Normally, you would only make a class function a member function if it needed to operate with an instance of the class. If it fits as part of the class but doesn't need to operate with an instance of the class, make it static.
Function A::f() is not a static member. So it requires an implicit object parameter that is this. So you cannot call it without specifying an object of class A. That is the call should look like

a.f( 1.0 );

where a - is object of A.
Thanks for the info. In my main.cpp file, I create an instance of class A (call it A_instance). When I create A_instance, I don't need to call f in main.cpp for what I'm trying to do here (but I do need to call f in main for other parts of my main.cpp file). (So, I don't think I can make f static.) In main, I also create an instance of class B, and I need to call g inside main (of course, using f in g). How do I create an instance of A in g?
For example in class A you are defining function f

public:
double f( double ) const;


Then in class B you are defining

1
2
3
4
5
6
double g( const A &a, double x )
{
...
   a.f( x );
...
}
I'm still getting errors. Here is what I have:

My header files:
1
2
3
4
5
class A
{
public:
double f(double);
}


1
2
3
4
5
6
#include "A.h"
class B
{
public:
double g(const A &a, double);
}


My cpp class files:
1
2
3
4
5
#include "A.h"
double A::f(double variable1)
{
// do stuff on variable1
}


1
2
3
4
5
6
#include "B.h"
#include "A.h"
double B::g(const A &a, double variable2)
{
a.f(variable2);
}


My main.cpp file:
1
2
3
4
5
6
7
#include "A.h"
#include "B.h"

A a; create an instance of class A
B b; create an instance of class B

b.g(const A &a); // call g from instance b, sending in a pointer to instance a 

Last edited on
Change last line to

b.g( a );
Even with changing that line in main.cpp, I still get an error. Here is the error:

passing 'const A' as 'this' argument of 'double A::f(double)' discards qualifiers
Last edited on
Please show your code.
Actually, I just fixed it. Everything seems to be running just fine, now. Here is my new code:

Class header files:
1
2
3
4
5
class A
{
public:
       double f(double);
}


1
2
3
4
5
6
#include "A.h"
class B
{
public:
       double g(A &a_dummy, double);
}


Class cpp files:
1
2
3
4
5
#include "A.h"
double A::f(double variable1)
{
       // do stuff on variable1
}


1
2
3
4
5
6
#include "A.h"
#include "B.h"
double B::g(A &a_dummy0, double variable2)
{
       a_dummy0.f(variable2);
}


Main.cpp:
1
2
3
4
5
6
7
#include "A.h"
#include "B.h"

A a;
B b;

b.g(a);
If function f() does not change the object it is better to define it as const

double f( double ) const;

and correspondingly in B

double g( const A &, double );
Thanks!
Topic archived. No new replies allowed.