| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3846 人关注过本帖
标题:std::ostream& operator<<(std::ostream& os, const Student stu),这个函数 ...
只看楼主 加入收藏
wengbin
Rank: 10Rank: 10Rank: 10
来 自:陕西西安
等 级:贵宾
威 望:19
帖 子:370
专家分:1846
注 册:2015-5-8
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:2 
std::ostream& operator<<(std::ostream& os, const Student stu),这个函数
程序代码:
std::ostream& operator<<(std::ostream& os, const Student& stu)//只要将&去掉,下面的代码就能正常跑,可是会创建临时变量,想做成直接引用的....
{
    using std::setw;
    using std::setfill;
    os << setw(stringWeith) << setfill(' ') << stu.name//报错成员name不可访问
        << setw(numberWeith) << setfill(' ') << stu.ID
        << setw(charWeith) << setfill(' ') << stu.sex << std::endl;
    return os;
}
2016-03-23 21:15
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:20 
stream必须是引用,它应该没有拷贝或赋值功能。
“成员name不可访问”你可以将这个函数声明为Student的友元(friend)
2016-03-24 09:29
wengbin
Rank: 10Rank: 10Rank: 10
来 自:陕西西安
等 级:贵宾
威 望:19
帖 子:370
专家分:1846
注 册:2015-5-8
收藏
得分:0 
回复 2楼 rjsp
谢谢r版主,我想到原因了,应该是我没改友元函数申明时的函数原型......

[此贴子已经被作者于2016-3-24 15:31编辑过]

2016-03-24 15:27
快速回复:std::ostream& operator<<(std::ostream& os, const Student stu),这 ...
数据加载中...
 
   



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

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