-2

i wrote this code but friend function is not working(foodmoney and hobbymoney are not declare in my friend function. where is my Error here ?

#include <iostream>
using namespace std;
class myBase
{
private:
    int friendvar;
    int foodmoney;
    int hobbymoney;
public:
    void setdata();
    myBase(){friendvar=0;}
    friend void caldata(myBase &mbo);
};

void myBase::setdata()
{
    cout<<"Enter foodmoney :" ;cin>>foodmoney;
    cout<<"enter hoobymoney:";cin>>hobbymoney;
}

void caldata(myBase &mbo)
{
    mbo.friendvar=(foodmoney+hobbymoney)/2;
    cout<<mbo.friendvar<<endl;
}

int main()
{
    myBase baseobj;
    baseobj.setdata();
    myBase friends;
    caldata(friends);

    return 0;
}
WhozCraig
  • 65,258
  • 11
  • 75
  • 141

3 Answers3

3
mbo.friendvar=(foodmoney+hobbymoney);

should be

mbo.friendvar=(mbo.foodmoney+mbo.hobbymoney);

etc. etc.

Friend functions are not member functions, so they do not have special access to any particular object. You must specify which object you wish to access (by using mbo in your case).

Having said that I can't see any good reason why caldata is a friend function. Why not make it a regular member function? Or maybe you should make it a friend function with two arguments? It's hard to say what you're trying to achieve here.

WhozCraig
  • 65,258
  • 11
  • 75
  • 141
john
  • 85,011
  • 4
  • 57
  • 81
  • The more obtuse question in this case, what does the OP honestly think is currently held as member-values in the `friends` object, which has never had `friends.setdata()` invoked prior to being passed down to the free-function `caldata()`? No members are specified in any initializer list of the constructor of `myBase`, and thus all members are default-initialized, which means nothing in this case. The values are indeterminate (save for `friendvar`, which is value-assigned in the constructor body). Thus the OP can expect indeterminate junk for his final output of the program as-written. – WhozCraig Mar 24 '13 at 07:31
  • @WhozCraig It could be an excerpt of his real code, and no longer make sense for that reason. We do always tell posters to cut down their code to better illustrate the problem. – john Mar 24 '13 at 07:36
  • I wouldn't bet on it in this case, factoring in what is presented and what the actual problem is. – WhozCraig Mar 24 '13 at 07:38
1

change

 cin>>foodmoney;

to

cin>>mbo.foodmoney;

and change

cin>>hobbymoney;

to

cin>>mbo.hobbymoney;
Deepu
  • 7,592
  • 4
  • 25
  • 47
0

Your code doesn't compile at all. Fix is described already. But to make ti complete: As you have referenced friendvar using mbo.friendvar, do the same for foodmoney and hobbymoney.

Also you have not initialized foodmoney and hobbymoney in your constructor. And in your call to caldata(friends); you are therefor getting random result printed on screen. Last: you instantiate new object with myBase friends; so this is completely new object that is not affected by previous call of setdata().

Your low level requirements for this function are not defined in your question, I cannot help more.

Sany
  • 103
  • 2
  • 8