How to have classes working together

Mar 25, 2017 at 9:51pm
I'm getting error codes -
Line 20 - error: 'text_output' was not declared in this scope
Line 28 - error: 'class B' has no member named 'change_class'
Line 26 - warning: unused variable 'first_class'

I guess line 26 is not needed.

But how do I get my classes to work together?

Thank you.

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
#include <iostream>

using namespace std;

class A
{
public:
    void text_output ()
    {
        cout << "Went from class B to class A." << endl << endl;
    }

};

class B
{
public:
    void change_class ()
    {
        text_output ();
    }
};

int main ()
{
    class A first_class;
    class B second_class;
    second_class.change_class ();

    return 0;
}
Mar 25, 2017 at 10:11pm
Still need help on Line 20.

Never-mind about the Line 28 error. I retyped it and ran the compiler again, and that error went away.

Not sure why Line 26 isn't needed.
Last edited on Mar 26, 2017 at 12:18am
Mar 26, 2017 at 12:16pm
Still need help on Line 20.

to call a non-static member function you'd need a calling object of type A:
1
2
3
4
5
void change_class ()
    {
        A a{};
        a.text_output ();
    }

and if text_output() was static you could have done:
1
2
3
4
5
6
7
8
class B
{
public:
    void change_class ()
    {
       A::text_output ();
    }
};
Mar 26, 2017 at 4:47pm
Tried both ways but still getting errors.

For your top solution I'm getting -
error: 'class A' has no member named 'text_output'

And the bottom solution I made text_output() static and made the changes, but -
error: 'text_output' is not a member of 'A'

Both errors indicate that 'text_output' is not a member of class 'A'.

Thanks for your help, this seems to be closer, but seems I'm still missing something.
Last edited on Mar 26, 2017 at 4:49pm
Mar 26, 2017 at 5:33pm
no way of knowing what you actually tried unless you post full code, these work as mentioned earlier:
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
#include <iostream>

using namespace std;

class A
{
public:
    void text_output ()
    {
        cout << "Went from class B to class A." << endl << endl;
    }

};

class B
{
public:
    void change_class ()
    {
        A a{};
        a.text_output ();
    }
};

int main ()
{
   // class A first_class;
    class B second_class;
    second_class.change_class ();

    return 0;
}



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
#include <iostream>

using namespace std;

class A
{
public:
    static void text_output ()
    {
        cout << "Went from class B to class A." << endl << endl;
    }

};

class B
{
public:
    void change_class ()
    {
        A::text_output ();
    }
};

int main ()
{
 //   class A first_class;
    class B second_class;
    second_class.change_class ();

    return 0;
}
Mar 26, 2017 at 5:35pm
This works on the Shell.
http://cpp.sh/5g3x
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
#include <iostream>

using namespace std;

class A
{
public:
    void text_output ()
    {
        cout << "Went from class B to class A." << endl << endl;
    }

};

class B
{
public:
    void change_class ()
    {
        A a;
        a.text_output ();
    }
};

int main ()
{
    class B second_class;
    second_class.change_class ();

    return 0;
}
Mar 26, 2017 at 6:16pm
Are you just trying to get your classes to work together or are you trying to get class B to inherit from class A?
Mar 26, 2017 at 10:50pm
Got it working. Somehow on my copy I misspelled text_output on line 8, hence the errors.

Thank you so much for helping me with this. So this is very similar to what we do in main. But now you can assign any function to any class, not just from int main ().

My classes are now working together! I know very vaguely of inheritance and friends, but not ready to learn about those yet. I also don't know about static and why the second option works.

Thanks again for teaching me to have my classes working together. Awesome!
Mar 27, 2017 at 1:40am
I also don't know about static and why the second option works

http://www.learncpp.com/cpp-tutorial/812-static-member-functions/
Topic archived. No new replies allowed.