0

why friend function not working in C++?

// compiler show error. // https://www.sololearn.com/compiler-playground/cno6jtPGQqyW #include <iostream> using namespace std; class B; class A { private: void printName() { cout << "xyz\n"; } void printWeight() { cout << "40kg\n"; } friend void B::showInfo(A); }; class B { public: void showInfo(A obj) { obj.printName(); obj.printWeight(); } }; int main() { A objA; B objB; objB.showInfo(objA); return 0; }

24th Sep 2023, 2:10 PM
Rishi
Rishi - avatar
4 Answers
+ 4
Rishi , Lots of mistake in your code.. #Missing class A declaration.. #For fix the error, need to reorder the class declarations so that class B is declared before class A. See this modified code.. https://code.sololearn.com/cwt53IJ16zQ9/?ref=app
24th Sep 2023, 2:32 PM
Darpan kesharwani🇮🇳[Inactive📚]
Darpan kesharwani🇮🇳[Inactive📚] - avatar
+ 1
Rishi Try again..
24th Sep 2023, 3:11 PM
Darpan kesharwani🇮🇳[Inactive📚]
Darpan kesharwani🇮🇳[Inactive📚] - avatar
0
Worked!
24th Sep 2023, 3:27 PM
Rishi
Rishi - avatar
0
Aziz
25th Sep 2023, 6:22 PM
Abdulaziz Aziz
Abdulaziz Aziz - avatar