C++重载运算符中关于友元函数的问题!

Anysysjgw 2012-02-29 12:18:26
用C++练习重载运算符<<和>>时,写了两个文件:overload.h和overload.cpp。
写了一个Time类,在overload.h中的声明是:
class Time {
private:
int hour;
int minute;
int sec;
public:
friend istream& operator>>(istream&,Time &);
friend ostream& operator<<(ostream&,Time &);
};

在overload.cpp中的定义是:
istream& operator>>(istream& input,Time &t) {
cout<<"input hour,minute and second of a time:";
input>>t.hour>>t.minute>>t.sec;
return input;
}
ostream& operator<<(ostream& output,Time &t) {
output<<t.hour<<":"<<t.minute<<":"<<t.sec;
return output;
}

IDE用的是VS2010,编译的时候出错,提示说hour/minute/sec不可访问。我写的友元函数的声明和定义再哪地方出错了?
...全文
174 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
Binzo 2012-03-03
  • 打赏
  • 举报
回复
#include <iostream>
using std::istream;
using std::ostream;
using std::cout;
using std::cin;

class Time {
public:
Time(int h, int m, int s){
hour = h;
minute = m;
sec = s;
}
private:
int hour;
int minute;
int sec;
public:
friend istream& operator >>(istream&,Time &);
friend ostream& operator <<(ostream&,Time &);
};

istream& operator>>(istream& input,Time &t) {
cout<<"input hour,minute and second of a time:";
input>>t.hour>>t.minute>>t.sec;
return input;
}
ostream& operator<<(ostream& output,Time &t) {
output<<t.hour<<":"<<t.minute<<":"<<t.sec;
return output;
}

int main(){
Time t(1, 2, 3);
cin >> t;
cout << t;
system("pause");
return 0;
}
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 anysysjgw 的回复:]
引用 8 楼 lou0378 的回复:
你没有使用using声明;加上:using namespace std;
我加了的。。。现在报的错是我重载的函数无法访问Time类里的私有成员变量,但我看我友元函数的定义没有错的,所以困惑就在这里
[/Quote]

如果加了using namespace std;声明的话,你的代码是没问题的,可以运行的,我是在vs2008下运行的,能得出正确的结果,你把错误贴出来,看看是不是其他错误引起的。
Anysysjgw 2012-03-03
  • 打赏
  • 举报
回复
问题解决!谢谢各位的帮助~错误原因是这样的:
在overload.cpp文件里,我写了
include <iostream>
include "overload.h
using namespace std;"

而在头文件overload.h里,我只声明了Time类,顶部没有写
using namespace std;
/*或者用
using std::istream;
using std::ostream;*/
因此造成了编译器对Time类里面声明的两个友元函数中的istream和ostream识别不出(可vs2010特有的下划线报错功能并没有对这个地方进行报错额...+_+)。
maoloverme1 2012-03-02
  • 打赏
  • 举报
回复
加上
#include <iostream>
using namespace std;
后代码在VC6中编译没有错误啊。
没有在vs2010下试过,但是我想应该不会有问题。
是不是是别的什么代码引起的错误。
Anysysjgw 2012-03-02
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 lou0378 的回复:]
你没有使用using声明;加上:using namespace std;
[/Quote]我加了的。。。现在报的错是我重载的函数无法访问Time类里的私有成员变量,但我看我友元函数的定义没有错的,所以困惑就在这里
pathuang68 2012-03-01
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 anysysjgw 的回复:]

1楼:我将两个文件合了一下,结果还是不行。
2楼:我已经include头文件了。
3楼:应该怎么clean呢?
[/Quote]

菜单build->clean solution(或者clean “项目名称”)
Anysysjgw 2012-03-01
  • 打赏
  • 举报
回复
1楼:我将两个文件合了一下,结果还是不行。
2楼:我已经include头文件了。
3楼:应该怎么clean呢?
  • 打赏
  • 举报
回复
下面是你的原码,然后加上using声明,就可以了

using std::istream;
using std::ostream;
using std::cout;

class Time {
private:
int hour;
int minute;
int sec;
public:
friend istream& operator >>(istream&,Time &);
friend ostream& operator <<(ostream&,Time &);
};

istream& operator>>(istream& input,Time &t) {
cout<<"input hour,minute and second of a time:";
input>>t.hour>>t.minute>>t.sec;
return input;
}
ostream& operator<<(ostream& output,Time &t) {
output<<t.hour<<":"<<t.minute<<":"<<t.sec;
return output;
}

  • 打赏
  • 举报
回复
你没有使用using声明;加上:using namespace std;
Anysysjgw 2012-03-01
  • 打赏
  • 举报
回复
唉,清理了也不行啊。。。还是报错
idoing_xsf 2012-02-29
  • 打赏
  • 举报
回复
不会出错,我用vs2010运行过没有错的,你用下面main试下:
[code=c/c++]
int main(void)
{
Time one;
cin>>one;
cout<<one<<endl;
return 0;
}
[/code]
不行把.h和.cpp文件和一个文件试下。
pengzhixi 2012-02-29
  • 打赏
  • 举报
回复
编译器问题
quwei197874 2012-02-29
  • 打赏
  • 举报
回复
没包含头文件?
程序员小迷 2012-02-29
  • 打赏
  • 举报
回复
只可能编译器用了以前的代码,需要重新clean,再编译;
vs2010经常干这事。。
xcoce也经常干这事。。

64,662

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧