我试图通过下面的朋友函数访问该类的私有成员我的代码是代码是完整的
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
class time;
class date{
void friend mixdatetime(date &c, time &d);
int day;
int month;
int year;
public:
date(int day, int month, int year){
this->day = day;
this->month = month;
this->year = year;
}};
class time{
int hours;
int minutes;
int seconds;
public:
time(int hours, int minutes, int seconds){
this->hours = hours;
this->minutes = minutes;
this->seconds = seconds;}
void print(){};} ;
void mixdatetime(date &c, time &d){
c.day; // accessable
// why //d.minutes // inacccess able };};
在此代码中,当我尝试访问d.minutes或d.hours //我不能,因为它无法访问为什么?我无法访问私人会员,请告诉我适当的解决方案
答案 0 :(得分:1)
为什么d.minutes无法访问
因为minutes
对于time
类是私有的,并且您没有将mixdatetime
声明为类time
的朋友。将友谊声明添加到班级time
:
friend void mixdatetime(date &c, time &d);