How to access function that use another class?

Oct 17, 2020 at 1:39pm
I use a class in another class like this :

a.hpp

1
2
3
4
Class A {
    public:
       void foo();
};

a.cpp

1
2
#include "a.hpp"
void A::foo() {}


b.hpp

1
2
3
4
5
#include "a.hpp"
class B {
   public:
      float stuff(A& a);
};

b.cpp

1
2
#include "b.hpp"
float B::stuff(A& a) { a.stuff(); return...}



How should call stuff(A& a) function in another place of the B class? when I Call
float take = stuff(A& a);
I Get this error: Symbol 'a' could not be resolved
Last edited on Oct 17, 2020 at 1:40pm
Oct 17, 2020 at 1:53pm
Consider this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class A {
	public:
	   void foo();
};

void A::foo() {}

class B {
public:
	float stuff(A& a);
};

float B::stuff(A& a) {
	a.foo();

	return 0;
}

int main()
{
	A aa;
	B bb;

	bb.stuff(aa);

}


which compiles OK VS 2019
Oct 17, 2020 at 6:13pm
I want to call

float take = stuff(A& a);

Also in B class, is it possible?
Last edited on Oct 17, 2020 at 6:13pm
Oct 17, 2020 at 6:23pm
That isn't valid syntax. But I'm not sure what your question is.
float stuff(A& a); is a member function of the B class. Yes, you can call it from within the B class, but your B class only has one function to begin with, so you'd just be calling it recursively.
Last edited on Oct 17, 2020 at 6:25pm
Oct 17, 2020 at 6:35pm
Sorry about my language, my B class has another function and I want to use float stuff(A& a); in it but I get this error: Symbol 'a' could not be resolved

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class B {
public:
	float stuff(A& a);
        void  reuse();
};

float B::stuff(A& a) {
	a.foo();

	return 0;
}

void B:: reuse(){

float take = stuff(A& a);

}
Last edited on Oct 17, 2020 at 6:35pm
Oct 17, 2020 at 7:00pm
Firstly, stop putting everything into different files. You won't benefit from this until your project is large enough that it must be organized this way.

Secondly, tell us about the situation that prompted this question. What are you doing, and why do you think this is the solution?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <cstdio>

struct A { void foo() { std::puts("A::foo"); } };

class B
{
  A a_;
public:
  void stuff(A& a) 
  { 
    a_ = a;
    a_.foo();
  }
 
  void reuse() { a_.foo(); }
};

int main()
{
  A a;
  B b;
  
  b.stuff(a);
  b.reuse();
}


Last edited on Oct 17, 2020 at 7:01pm
Oct 17, 2020 at 9:39pm
What has already been said/asked.

The syntax that you have used has same error as this:
1
2
3
4
5
void answer( int& z ) { z = 42; }

int main() {
  answer( int& z ); // syntax error
}


A member function of class can call member function of a class.
That should be quite obvious in:
1
2
3
4
float B::stuff(A& a) {
	a.foo();
	return 0;
}

Where member B::stuff calls member A::foo.
You do need an object, whose member is called. Hence the a.foo();

Members of same class are implicitly called on same object, but you can be more explicit with this->:
1
2
3
4
void B::reuse(){
  // ....
  float take = this->stuff( x );
}

The B::stuff takes a reference to an object as parameter. Therefore, it must be called with an object as argument:
1
2
3
4
void B::reuse(){
  A x;
  float take = stuff( x );
}


Oct 18, 2020 at 7:16am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>

class A
{
    public:
    void foo() {std::cout << "A::foo\n"; }
};

class B
{
   public:
    static float stuff(A& a) { std::cout << "B::stuff\n"; a.foo(); return 32; }
};

int main()
{
    A a;
    a.foo();
    B::stuff(a);
    std::cout << B::stuff(a) << '\n';
    
    B b;
    std::cout << b.stuff(a) << '\n';
    
    return 0;
}

[output]
A::foo
B::stuff
A::foo
B::stuff
A::foo
32
B::stuff
A::foo
32
Program ended with exit code: 0[/output]
Oct 18, 2020 at 8:03am
@keskiverto thank you my problem resolve with your solution

And thank you all for your help
Topic archived. No new replies allowed.