这是咋回事啊?友元函数和私有变量问题

selooloo selooloo 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
代码如下:

#include <iostream>
#include <cctype>
#include <cstdlib>
//#include "dtime.h"
using namespace std;

class DigitalTime
{
public:
friend bool operator ==(const DigitalTime &time1,const DigitalTime& time2);
DigitalTime(int the_hour,int the_minute);
DigitalTime();
void advance(int minutes_added);
void advance(int hours_added,int minutes_added);
friend istream& operator >>(istream& ins,DigitalTime the_object);
friend ostream& operator <<(ostream& outs,const DigitalTime the_object);
private:
int hour;
int minute;
};

void read_hour(istream& ins,int& the_hour);
void read_minute(istream& ins,int& the_minute);
bool operator ==(const DigitalTime& time1,const DigitalTime& time2)
{
return (time1.hour==time2.hour && time2.minute==time2.minute);
}

DigitalTime::DigitalTime(int the_hour,int the_minute)
{
if(the_hour<0 || the_minute<0)
{
cout<<"Illegal time!\n";
system("pause");
exit(1);
}
hour=the_hour;
minute=the_minute;
}

DigitalTime::DigitalTime():hour(0),minute(0)
{}

void DigitalTime::advance(int hours_added,int minutes_added)
{
int new_hours,new_minutes;
new_minutes=minute+minutes_added;
minute=new_minutes%60;
new_hours=hour+hours_added+new_minutes/60;
hour=new_hours%24;
}

void DigitalTime::advance(int minutes_added)
{
int gross_minutes=minute+minutes_added;
minute=gross_minutes%60;
int hour_adjustment=gross_minutes/60+hour;
hour=hour_adjustment%24;
}

ostream& operator <<(ostream& outs,const DigitalTime& the_object)
{
outs<<the_object.hour<<":";
if(the_object.minute<10)
outs<<'0';
outs<<the_object.minute;
return outs;
}

istream& operator >>(istream& ins,DigitalTime& the_object)
{
read_hour(ins,the_object.hour);
read_minute(ins,the_object.minute);
return ins;
}

int digit_to_int(char c)
{
return (c-'0');
}

void read_hour(istream& ins,int& the_hour)
{
char c1,c2;
ins>> c1>>c2;
if(!(isdigit(c1) && isdigit(c2) || c2==':'))
{
cout<<"Error illegal input to read_hour.\n";
system("pause");
exit(1);
}
if(c2==':')
the_hour=digit_to_int(c1);
else
{
the_hour=digit_to_int(c1)*10 + digit_to_int(c2);
ins>>c2;
if(c2!=':')
{
cout<<"Error illegal input to read_minute.\n";
system("pause");
exit(1);
}
}
if(the_hour>24 || the_hour<0)
{
cout<<"Error illegal input to read_hour.\n";
system("pause");
exit(1);
}
}

void read_minute(istream& ins,int& the_minute)
{
char c1,c2;
if(!isdigit(c1) || !isdigit(c2))
{
cout<<"Error illegal input to read_minute.\n";
system("pause");
exit(1);
}
the_minute=digit_to_int(c1)*10 + digit_to_int(c2);
if(the_minute<0 || the_minute>60)
{
cout<<"Error illegal input to read_minute.\n";
system("pause");
exit(1);
}
}


错误如下:

15 D:\dev\c++\dtime.h
`int DigitalTime::hour' is private
16 D:\dev\c++\dtime.h
`int DigitalTime::minute' is private

别的函数都可以访问私有变量,就
friend istream& operator >>(istream& ins,DigitalTime the_object);
friend ostream& operator <<(ostream& outs,const DigitalTime the_object);
这两个函数不行,实在是蹊跷,眼睛快看爆掉了也没看有出啥错误,各位帮帮忙
...全文

相关推荐

友元函数和友元类_友元函数用法_

私有成员只能在类的成员函数内部访问,如果想在别处访问对象的私有成员,只能通过类提供的接口(成员函数)间接地进行。这固然能够带来数据隐藏的好处,利于将来程序的扩充,但也会增加程序书写的麻烦。

kikikuka

C++之友元:友元函数和友元类详解

一、友元介绍我们知道,类的成员函数可以访问同类的其他成员函数,包括公有、私有和保护成员。而类的外部函数只能访问类的公有成员。友元是一种允许非类成员函数访问类的非公有成员的一种机制。可以把一个函数指定为类的友元,也可以把整个类指定为另一个类的友元。 友元函数友元类 二、友元函数友元函数在类作用域外定义,但它需要在类体中进行说明为了与该类的成员函数加以区别,定义的方式是在类中用关键字friend说明该函数,格式如下: friend  类型 友元函数名(参数表);友元的作用在于提高程序的运行效率 友元函数注意事项:1、友元函数不是类的成员函数,在函数体中访问对象的成员,必须用对象名加运算符“.”加对

weixin_38558870

C++友元函数及友元类全总结

本文档详细介绍了C++友元函数及友元类,方便大家对比学习

灬風無痕灬

友元函数详细讲解

友元函数是指某些虽然不是类成员却能够访问类的所有成员的函数。类授予它的友元特别的访问权。通常同一个开发者会出于技术和非技术的原因,控制类的友元和成员函数(否则当你想更新你的类时,还要征得其它部分的拥有者的同意)。

wangbin_147

c++访问私有private成员变量的常用方法

主要介绍了c++访问私有private成员变量的常用方法,需要的朋友可以参考下

weixin_38626075
评论 7
收藏 已收藏
分享
返回顶部
selooloo
C++ 语言 65,214 成员 250,514 内容