| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 459 人关注过本帖
标题:关于一个友员函数的问题!
取消只看楼主 加入收藏
hlershi
Rank: 1
等 级:新手上路
帖 子:6
专家分:1
注 册:2009-8-11
结帖率:100%
收藏
已结贴  问题点数:10 回复次数:0 
关于一个友员函数的问题!
#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)
搜索更多相关主题的帖子: 友员函数问题 
2009-08-24 12:49
快速回复:关于一个友员函数的问题!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.021905 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved