为什么重载ostream不能编译通过?vc++6

NesTa_xP 2008-11-21 03:06:56
#include<iostream>
using namespace std;
class A{

int x;
public:
A(int i=0):x(i){}
const A operator+(const A& lhs) const
{
return A(x+lhs.x);
}
friend ostream& operator<<(ostream&,const A&);
};
ostream& operator<<(ostream& os,const A& a)
{
os<<a.x;
return os;
}
int main()
{
A a(3);
A b,c;
cout<<a<<endl;
cout<<b<<endl;
cout<<c<<endl;
c=a+b;
cout<<a<<endl;
cout<<b<<endl;
cout<<c<<endl;
}


--------------------Configuration: 12_6 - Win32 Debug--------------------
Compiling...
12_6.cpp
C:\Documents and Settings\Owner\My Documents\Thinking_in_C++作业\12_6.cpp(16) : error C2248: 'x' : cannot access private member declared in class 'A'
C:\Documents and Settings\Owner\My Documents\Thinking_in_C++作业\12_6.cpp(5) : see declaration of 'x'
C:\Documents and Settings\Owner\My Documents\Thinking_in_C++作业\12_6.cpp(23) : error C2593: 'operator <<' is ambiguous
C:\Documents and Settings\Owner\My Documents\Thinking_in_C++作业\12_6.cpp(24) : error C2593: 'operator <<' is ambiguous
C:\Documents and Settings\Owner\My Documents\Thinking_in_C++作业\12_6.cpp(25) : error C2593: 'operator <<' is ambiguous
C:\Documents and Settings\Owner\My Documents\Thinking_in_C++作业\12_6.cpp(27) : error C2593: 'operator <<' is ambiguous
C:\Documents and Settings\Owner\My Documents\Thinking_in_C++作业\12_6.cpp(28) : error C2593: 'operator <<' is ambiguous
C:\Documents and Settings\Owner\My Documents\Thinking_in_C++作业\12_6.cpp(29) : error C2593: 'operator <<' is ambiguous
C:\Documents and Settings\Owner\My Documents\Thinking_in_C++作业\12_6.cpp(30) : warning C4508: 'main' : function should return a value; 'void' return type assumed
执行 cl.exe 时出错.

12_6.exe - 1 error(s), 0 warning(s)

...全文
181 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
霍大脚 2008-11-21
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 bitxinhai 的回复:]
我的vc6.0编译通过!!
没有错误的!!!
vc怎么了!!
还是很好用的吗!!!!
[/Quote]

他的VC有设置有问题的啊
bitxinhai 2008-11-21
  • 打赏
  • 举报
回复
我的vc6.0编译通过!!
没有错误的!!!
vc怎么了!!
还是很好用的吗!!!!
霍大脚 2008-11-21
  • 打赏
  • 举报
回复

看他的错误信息就知道你现在用VC6.0对友元函数支持有问题
霍大脚 2008-11-21
  • 打赏
  • 举报
回复

2008能通过下面是结果:
3
0
0
3
0
3
请按任意键继续. . .

换点新的吧,在6.0好多新东西都实现不了。。
wgq2633 2008-11-21
  • 打赏
  • 举报
回复
在class A的定义之前添加以下代码:

class A; //申明类A
ostream& operator<<(ostream&,const A&); //申明函数operator<<

VC6.0对友元支持确实有问题,在vc高版本中你原来的代码就能通过的
就呆在云上 2008-11-21
  • 打赏
  • 举报
回复
没有问题
vc2008
airtrack 2008-11-21
  • 打赏
  • 举报
回复
vc6早就该扔了。用2005、2008或gcc。
lann64 2008-11-21
  • 打赏
  • 举报
回复
用vc6就别用友元。vc6对友元支持上有bug
qqwx_1986 2008-11-21
  • 打赏
  • 举报
回复
你的代码是正确的
有时建议用dev编译
vc6对友元几乎没得支持....
taodm 2008-11-21
  • 打赏
  • 举报
回复
扔了VC6吧,不要用它来浪费生命了。

65,211

社区成员

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

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