How to make friend classes if they in different files .h | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How to make friend classes if they in different files .h

i have two classes a.h and b.h . and without .cpp files. because a code is in .h files . And i cant understand how to make them a friends. in function: public: void damage(a &obj){ obj.hp-=dmg; } Compile said me " a is not define"

14th Oct 2018, 1:04 PM
Otakoe
Otakoe - avatar
4 Answers
+ 2
Did you include your a.h in b.h? #include "a.h" Better practise to devide class declaration (a.h) and class description (a.cpp).
15th Oct 2018, 10:52 PM
microBIG
microBIG - avatar
+ 1
yes, i agree with you. but how to do friendly classes when i make descriptiin and declaration in same file. when i #include a.h in b.h and #include b.h in a.h , compile said me that included class define self.
15th Oct 2018, 11:05 PM
Otakoe
Otakoe - avatar
+ 1
Try to use forward declaration. class A; class B { ... }; class A { ... };
16th Oct 2018, 6:15 AM
microBIG
microBIG - avatar
+ 1
yes it work when write all in one file .cpp. is this possible by using only .h files for classes?
16th Oct 2018, 6:32 AM
Otakoe
Otakoe - avatar