"<<"重载(声明为友元)错在哪里? ,在线。

mjohhh 2003-04-28 08:02:56
#include <iostream>
#include <string>
using namespace std;

class U{
public:
U(int m,int n){
i=m; //(12)行
j=n; //(13)
}
protected:
int i;
int j;
friend ostream& operator << (ostream& oo,U u); //友元
};

ostream& operator << (ostream& oo,U u){
oo << u.i << u.j; //(21)
return oo;
}

void main()
{
U u(3,5);
cout << u; //(28)
}

(21) : error C2248: 'i' : cannot access protected member declared in class 'U'
(12) : see declaration of 'i'
(21) : error C2248: 'j' : cannot access protected member declared in class 'U'
(13) : see declaration of 'j'
(28)error C2593: 'operator <<' is ambiguous
...全文
37 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
diabloqin 2003-04-28
  • 打赏
  • 举报
回复
楼上说的很清楚了
woxihuanbohe 2003-04-28
  • 打赏
  • 举报
回复
哦 我想你用的是那个编译器?
vc6吗?
也许你需要修改下头文件
#include <iostream>//<iostream.h>
#include <string>//<string.h>
using namespace std;//去掉
lifanxi 2003-04-28
  • 打赏
  • 举报
回复
先声明我没有仔细看您的程序,然后说你的程序在我这里编译没问题。
再告诉你可能是VC6的BUG引起的。
最后告诉你解决方案:
把#include <iostream>
using namespace std;
改成#include <iostream.h>
或者为VC6打上SP5补丁。
或者别用VC6编译器。

24,854

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 工具平台和程序库
社区管理员
  • 工具平台和程序库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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