下面这个程序为什么编译时显示无法访问private定义的私有数据成员?他们不是友元函数吗?
问题出在>>重载的函数。。。。#include"stdafx.h"#include<iostream>usingnamespacestd;classTime{public...
问题出在>>重载的函数。。。。
#include "stdafx.h"
#include<iostream>
using namespace std;
class Time{
public:
Time(int h=0,int m=0,int s=0);
friend ostream & operator<<(ostream &output, Time &A);
friend istream & operator<<(istream &input,Time &A);
private:
int hour;
int minute;
int second;
};
Time::Time(int h,int m,int s)
{hour=h;
minute=m;
second=s;
}
ostream &operator<<(ostream &output, Time &A)
{output<<A.hour<<":";
output<<A.minute<<":";
output<<A.second<<":";
return output;
}
istream &operator>>(istream &input,Time &A)
{
input>>A.hour;
input>>A.minute;
input>>A.second;
return input;
}
int _tmain(int argc, _TCHAR* argv[])
{
Time A;
cout<<"输入所需要的时间:"<<endl;
cin>>A;
cout<<"输出:";
cout<<A<<endl;
return 0;
} 展开
#include "stdafx.h"
#include<iostream>
using namespace std;
class Time{
public:
Time(int h=0,int m=0,int s=0);
friend ostream & operator<<(ostream &output, Time &A);
friend istream & operator<<(istream &input,Time &A);
private:
int hour;
int minute;
int second;
};
Time::Time(int h,int m,int s)
{hour=h;
minute=m;
second=s;
}
ostream &operator<<(ostream &output, Time &A)
{output<<A.hour<<":";
output<<A.minute<<":";
output<<A.second<<":";
return output;
}
istream &operator>>(istream &input,Time &A)
{
input>>A.hour;
input>>A.minute;
input>>A.second;
return input;
}
int _tmain(int argc, _TCHAR* argv[])
{
Time A;
cout<<"输入所需要的时间:"<<endl;
cin>>A;
cout<<"输出:";
cout<<A<<endl;
return 0;
} 展开
展开全部
private 是类本身可以访问,其他类,包括子类都无法访问,
protected是友元,
public 是公开,所有都可以,
貌似还有其他关键字,但常用的就这三个.
protected是友元,
public 是公开,所有都可以,
貌似还有其他关键字,但常用的就这三个.
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
friend istream & operator>>(istream &input,Time &A);
你打错了
#include<iostream>
using namespace std;
class Time{
public:
Time(int h=0,int m=0,int s=0);
friend ostream & operator<<(ostream &output, Time &A);
friend istream & operator>>(istream &input,Time &A);
private:
int hour;
int minute;
int second;
};
Time::Time(int h,int m,int s)
{hour=h;
minute=m;
second=s;
}
ostream &operator<<(ostream &output, Time &A)
{output<<A.hour<<":";
output<<A.minute<<":";
output<<A.second<<":";
return output;
}
istream &operator>>(istream &input,Time &A)
{
input>>A.hour;
input>>A.minute;
input>>A.second;
return input;
}
int main()
{
Time A;
cout<<"输入所需要的时间:"<<endl;
cin>>A;
cout<<"输出:";
cout<<A<<endl;
return 0;
}
你打错了
#include<iostream>
using namespace std;
class Time{
public:
Time(int h=0,int m=0,int s=0);
friend ostream & operator<<(ostream &output, Time &A);
friend istream & operator>>(istream &input,Time &A);
private:
int hour;
int minute;
int second;
};
Time::Time(int h,int m,int s)
{hour=h;
minute=m;
second=s;
}
ostream &operator<<(ostream &output, Time &A)
{output<<A.hour<<":";
output<<A.minute<<":";
output<<A.second<<":";
return output;
}
istream &operator>>(istream &input,Time &A)
{
input>>A.hour;
input>>A.minute;
input>>A.second;
return input;
}
int main()
{
Time A;
cout<<"输入所需要的时间:"<<endl;
cin>>A;
cout<<"输出:";
cout<<A<<endl;
return 0;
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询