| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1269 人关注过本帖
标题:[求助]输出流重载问题?
只看楼主 加入收藏
bill8888
Rank: 1
等 级:新手上路
帖 子:97
专家分:0
注 册:2007-3-10
收藏
 问题点数:0 回复次数:7 
[求助]输出流重载问题?

程序是这样的:
#include<iostream>
#include<iomanip>
using namespace std;

class Date{
int year, month, day;
public:
Date(int y=2000, int m=1, int d=1); // 设置默认参数
Date(const string& s); // 重载
bool isLeapYear()const;
friend ostream& operator<<(ostream& o, const Date& h);
};
Date::Date(const string& s){
year = atoi(s.substr(0,4).c_str());
month = atoi(s.substr(5,2).c_str());
day = atoi(s.substr(8,2).c_str());
}
Date::Date(int y, int m, int d){ year=y,month=m,day=d; }
bool Date::isLeapYear()const{
return (year % 4==0 && year % 100 )|| year % 400==0;
}
ostream& operator<<(ostream& o, const Date& h)
{
o<<setfill('0')<<setw(4)<<h.year<<'-'<<setw(2)<<h.month<<'-'
<<setw(2)<<h.day<<'\n'<<setfill(' ');
return o;
}
int main(){
Date c("2005-12-28");
Date d(2003,12,6);
Date e(2002); // 默认两个参数
Date f(2002,12); // 默认一个参数
Date g; // 默认三个参数
cout<<c<<d<<e<<f<<g;
return 0;
}

错误是:
--------------------Configuration: sadf - Win32 Debug--------------------
Compiling...
sadf.cpp
F:\学习资料\编程\vc++\cpp文件\sadf.cpp(32) : error C2248: 'year' : cannot access private member declared in class 'Date'
F:\学习资料\编程\vc++\cpp文件\sadf.cpp(10) : see declaration of 'year'
F:\学习资料\编程\vc++\cpp文件\sadf.cpp(32) : error C2248: 'month' : cannot access private member declared in class 'Date'
F:\学习资料\编程\vc++\cpp文件\sadf.cpp(10) : see declaration of 'month'
F:\学习资料\编程\vc++\cpp文件\sadf.cpp(33) : error C2248: 'day' : cannot access private member declared in class 'Date'
F:\学习资料\编程\vc++\cpp文件\sadf.cpp(10) : see declaration of 'day'
F:\学习资料\编程\vc++\cpp文件\sadf.cpp(42) : error C2593: 'operator <<' is ambiguous
Error executing cl.exe.

sadf.obj - 4 error(s), 0 warning(s)


搞不明白,为什么说不能访类里的私有变量,明明可以的,这怎么解释啊?


[此贴子已经被作者于2007-4-29 23:38:03编辑过]

搜索更多相关主题的帖子: Date int const str 
2007-04-29 23:37
aipb2007
Rank: 8Rank: 8
来 自:CQU
等 级:贵宾
威 望:40
帖 子:2879
专家分:7
注 册:2007-3-18
收藏
得分:0 
http://bbs.bc-cn.net/viewthread.php?tid=133743&extra=&page=10#112525


看看这个,我想你遇到了一样的问题

编译器问题,换个gcc试试!

Fight  to win  or  die...
2007-04-30 00:24
bill8888
Rank: 1
等 级:新手上路
帖 子:97
专家分:0
注 册:2007-3-10
收藏
得分:0 

我在DEV C++里运行能够通过
真不知道VC++是怎么回事啊
居然友元函数不能访问类里的私有变量,有违标准C++的规定嘛


2007-04-30 00:35
virtual_man
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2007-5-13
收藏
得分:0 

二楼楼主无限循环在他给出的连接中有相关解决问题的方法.我对这个问题也想了是不是vc++6的一个bug?我觉得应该不是吧,在DEV C++能通过编译在vc++不能通过不能代表有bug吧,这得取决于编译器怎么设计的问题,我的专业不搞编译器,所以我声明我不专业.是这样的,我认为,在你的类中要重载插入和提取符,但在你类的定义中根本就没有该重载函数的声明,所以必须先声明,叫前向声明.而在做了重载函数的前向声明后,由于提取符重载函数用到该类,所以在重载函数的声明之前也还要类的声明.这一系列的前向声明是符合编译器的先声明后使用的原则的.所以这个只是编译器怎么设计的问题,应该并不是个bug.

2007-05-13 17:21
yuyunliuhen
Rank: 6Rank: 6
等 级:贵宾
威 望:20
帖 子:1435
专家分:0
注 册:2005-12-12
收藏
得分:0 
DEV的内核是GC++,它更加支持最新标准的C++,我愿意相信GC,虽然偶也用VC

Go confidently in the  directions of your dreams,live the life you have imagined!Just do it!
It is no use learning without thinking!
2007-05-13 17:30
raulxxyuer
Rank: 1
等 级:新手上路
威 望:1
帖 子:178
专家分:0
注 册:2007-4-23
收藏
得分:0 

我很沒用,總是學不會遺忘,總是學不會割捨本不屬於我的東西。
2007-05-13 17:51
游乐园
Rank: 3Rank: 3
等 级:新手上路
威 望:6
帖 子:671
专家分:0
注 册:2006-11-1
收藏
得分:0 
老问题 给VC++ 打上sp6 就可以了

要不就做提前声明也能解决

...
using namespace std;

class Date;
ostream& operator<<(ostream& o, const Date& h);

class Date {
...

unicorn-h.spaces. ◇◆ sava-scratch.spaces.
2007-05-13 18:49
ningkli
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2008-11-14
收藏
得分:0 
可是为什么在c++里。用<iostream.h>代替<iostream>和using namespace std;却可以通过调试呢?
2008-11-14 14:11
快速回复:[求助]输出流重载问题?
数据加载中...
 
   



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

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