+ 4

I'm having a compilation error in this code, please can you solve this..

#include <iostream> using namespace std; class item { int sum,a,b; public: void getdata(); void sum() { sum=a+b; } }; void item :: getdata() { cout<<"enter value"; cin<<a<<b; } int main() { item x; cout<<"\nobject x "<<"\n"; x.getdata(); void sum(); cout<<sum; return 0; }

4th Apr 2017, 6:48 AM
PRIYA🙌
4 Respostas
+ 5
I finally got it....... #include <iostream> using namespace std; class xyz { int a,b,sum; public: xyz() { cin>>a>>b; sum=a+b; } void disp(); }; void xyz::disp() { cout<<"sum="<<sum; } int main() { xyz i; i.disp(); return 0; }
4th Apr 2017, 3:27 PM
PRIYA🙌
+ 4
Thanks....
4th Apr 2017, 12:55 PM
PRIYA🙌
+ 2
Can you post the error message? You cannot acces the member function sum by "void sum()" The member function sum should have a return value: int sum(){ return a+b; } int sum = x.sum(); cout<<sum;
4th Apr 2017, 7:08 AM
Daniel Thomalla
Daniel Thomalla - avatar
0
you variable sum and method sum bot have same identifier. change method sum to getsum() and it will work fine
4th Apr 2017, 7:40 AM
Keshave Jat
Keshave Jat - avatar