+ 1

Help me why this below code shows error !!

This code belongs to use of friend function : This is the code : #include <iostream> using namespace std; class bike; class cycle; class car{ int score; public : car (int a){ score=a; } friend void max(car,bike,cycle); }; class bike{ int score; public : bike (int b){ score=b; } friend void max(bike,cycle,car); }; class cycle{ int score; public : cycle (int c){ score=c; } friend void max(cycle,bike,car); }; void max(car c,bike b,cycle cy){ if(c.score>b.score && c.score>cy.score) { cout<<"Car wins"<<endl; } } int main() { bike b(10); cycle c(5); car cr(15); return 0; } https://code.sololearn.com/cAQNO4NVo7hl/?ref=app

27th Oct 2022, 8:06 AM
Kabilan K
Kabilan K - avatar
2 Respuestas
+ 4
Friend function declaration arguments are not matching with definition friend void max(car,bike,cycle); // use this in all classes.. max(cr,b,c); // call this in main..
27th Oct 2022, 9:51 AM
Jayakrishna 🇮🇳
+ 2
Perhaps it would be better that way (less likely to make a mistake).😎: #include <iostream> using namespace std; class Transport{ public: int s; void score(int arg){ s = arg; } }; class Car: public Transport {}; class Bike: public Transport {}; class Cycle: public Transport {}; void finish(Car c, Bike b, Cycle cy){ if(c.s>b.s && c.s>cy.s) cout<<"Car wins!"<<endl; else cout<<"Car loses."<<endl; } int main() { Bike b; b.score(10); Cycle cy; cy.score(5); Car c; c.score(15); finish(c,b,cy); return 0; }
27th Oct 2022, 1:38 PM
Solo
Solo - avatar