我寫了這個代碼,但好友功能無法正常工作(foodmoney和hobbymoney在我朋友的功能沒有申報。這裏是我的錯誤嗎?友元函數C++裏面
#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;
}
你應該寫somethi ng關於錯誤的更多問題 – 2013-03-24 07:31:06