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'
#include <iostream>
usingnamespace 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;
}
#include <iostream>
usingnamespace 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;
}
#include <iostream>
usingnamespace std;
class A
{
public:
staticvoid 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;
}
#include <iostream>
usingnamespace 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;
}
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!