关于一个友员函数的问题!
#include<iostream>#include<cstdlib>
#include<cctype>
using namespace std;
class Money
{
public:
friend Money add(Money amount1,Money amount2);
friend bool equal(Money amount1,Money amount2);//我已经把这个设为了友员函数为什么还为出现这样的错误
Money(long dollars ,int cents);
Money(long dollars);
Money();
double get_value();
void input(istream& ins);
void output(ostream& outs);
private:
long all_cents;
};
int digit_to_int(char c);
int main()
{
Money your_amount,my_amount(10,9),our_amount;
cout<<"请输入一个金额:";
your_amount.input(cin);
cout<<"你的金额是:";
your_amount.output(cout);
cout<<endl;
cout<<"我的金额是:";
my_amount.output(cout);
cout<<endl;
if(equal(your_amount,my_amount))
cout<<"你的钱和我的一样多:\n";
else
cout<<"我们一个人富点:\n";
our_amount=add(your_amount,my_amount);
your_amount.output(cout);
cout<<"+";
my_amount.output(cout);
cout<<" equals";
our_amount.output(cout);
cout<<endl;
return 0;
}
Money add(Money amount1,Money amount2)
{
Money temp;
temp.all_cents=amount1.all_cents+amount2.all_cents;
return temp;
}
bool equal(Money amount1, Money amount2)
{
return (amount1.all_cents==amount2.all_cents);
Text100.exe - 2 error(s), 0 warning(s)
}
Money::Money(long dollars):all_cents(dollars*100)
{}
Money::Money():all_cents(0)
{}
double Money::get_value()
{return (all_cents*0.01);}
void Money::input(istream& ins)
{
char one_char,decimal_point,digit1,digit2;
long dollars;
int cents;
bool negative;
ins>>one_char;
if(one_char=='-')
{
negative=true;ins>>one_char;
}
else negative=false;
ins>>dollars>>decimal_point>>digit1>>digit2;
if(one_char!='s'||decimal_point!='.'||!isdigit(digit1)||!isdigit(digit2))
{
cout<<"输入错误:\n";
exit(1);
}
cents=digit_to_int(digit1)*10+digit_to_int(digit2);
all_cents=dollars*100+cents;
if(negative)
all_cents=-all_cents;
}
void Money::output(ostream& outs)
{
long positive_cents,dollars,cents;
positive_cents=labs(all_cents);
dollars=positive_cents/100;
cents=positive_cents%100;
if(all_cents<0)
outs<<"-$"<<dollars<<'.';
else
outs<<"$"<<dollars<<'.';
if(cents<10)
outs<<'0';
outs<<cents;
}
int digit_to_int(char c)
{return (int(c)-int(0));}
错误:ext100.cpp
D:\My Documents\Text100.cpp(54) : error C2248: 'all_cents' : cannot access private member declared in class 'Money'
D:\My Documents\Text100.cpp(17) : see declaration of 'all_cents'
D:\My Documents\Text100.cpp(54) : error C2248: 'all_cents' : cannot access private member declared in class 'Money'
D:\My Documents\Text100.cpp(17) : see declaration of 'all_cents'
Error executing cl.exe.
Text100.exe - 2 error(s), 0 warning(s)